2012-10-04 9 views
23

पर accessdenied अपवाद फेंकता है मैं विंडोज 8 में फिर से प्रयास/रद्द संवाद बॉक्स को कार्यान्वित करने का प्रयास कर रहा हूं। संवाद बॉक्स पहली बार ठीक दिखाता है, लेकिन फिर से प्रयास करने पर और फिर से असफल होने पर, मुझे एक एक्सेस अस्वीकार अपवाद मिलता है ShowAsync को कॉल करने पर। मुझे नहीं पता क्यों, लेकिन कभी-कभी इसकी अजीब बात ठीक काम करती है और जब मैं ब्रेकपॉइंट डालता हूं तो मुझे अपवाद नहीं मिलता है। वास्तव में यहां अनजानMessageDialog ShowAsync दूसरे संवाद

यहां कोड है।

async void DismissedEventHandler(SplashScreen sender, object e) 
    { 
     dismissed = true; 
     loadFeeds(); 
    } 
    private async void loadFeeds() 
    { 
     await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => 
     { 
      try 
      { 
       RSSDataSource rssDataSource = (RSSDataSource)App.Current.Resources["RSSDataSource"]; 
       if (rssDataSource != null) 
       { 
        await rssDataSource.DownloadFeeds(); 
        await rssDataSource.GetFeedsAsync(); 
       } 

       AdDataSource ads = (AdDataSource)App.Current.Resources["AdDataSource"]; 

       if (ads != null) 
       { 
        await ads.DownloadAds(); 
       } 
       rootFrame.Navigate(typeof(HomePageView)); 

       Window.Current.Content = rootFrame; 
      } 
      catch 
      { 
       ShowError(); 
      } 

     }); 
    } 
    async void ShowError() 
    { 
     // There was likely a problem initializing 
     MessageDialog msg = new MessageDialog(CONNECTION_ERROR_MESSAGE, CONNECTION_ERROR_TITLE); 

     // Add buttons and set their command handlers 
     msg.Commands.Add(new UICommand(COMMAND_LABEL_RETRY, new UICommandInvokedHandler(this.CommandInvokedHandler))); 
     msg.Commands.Add(new UICommand(COMMAND_LABEL_CLOSE, new UICommandInvokedHandler(this.CommandInvokedHandler))); 
     // Set the command to be invoked when a user presses 'ESC' 
     msg.CancelCommandIndex = 0; 

     await msg.ShowAsync(); 
    } 

    /// <summary> 
    /// Callback function for the invocation of the dialog commands 
    /// </summary> 
    /// <param name="command">The command that was invoked</param> 
    private void CommandInvokedHandler(IUICommand command) 
    { 
     string buttonLabel = command.Label; 
     if (buttonLabel.Equals(COMMAND_LABEL_RETRY)) 
     { 
      loadFeeds(); 
     } 
     else 
     { 
      // Close app 
      Application.Current.Exit(); 
     } 
    } 

उत्तर

24

ठीक है, मैं एक त्वरित समाधान,

पाया एक IAsyncOperation वर्ग varialble

IAsyncOperation<IUICommand> asyncCommand = null; 

को परिभाषित करने और MessageDialog

asyncCommand = msg.ShowAsync(); 

की ShowAsync विधि के लिए सेट आदेश हैंडलर में पुनः प्रयास करने के लिए/पुनः प्रयास करें जांचें कि क्या asyncCommand नहीं है शून्य यदि आवश्यक हो तो अंतिम ऑपरेशन रद्द करें

if(asyncCommand != null) 
{ 
    asyncCommand.Cancel(); 
} 

कृपया मुझे बताएं कि इसके लिए कोई बेहतर तरीका है या नहीं।

+0

"अ-निरुपित स्थानीय चर 'asyncCommand' का उपयोग" हो रही रोकने के लिए, मैं आवंटित करने के लिए किया था जब इसे असाइन किया जाता है तो asyncCommand को शून्य करें। –

+1

साइड नोट: मैं एक एकल थ्रेड में अपने ही काम चल रहा है कुए थी और मैं एक बार में केवल एक धागे से एक ShowAsync कर रहा था। जाहिर है अगर एक शोएसिंक फ्रेम 1 में समाप्त होता है और दूसरा शोएसिंक फ्रेम 2 में शुरू होता है, तो एक यादृच्छिक एक्सेस अस्वीकृत त्रुटि पॉप हो सकती है: /। यद्यपि काम को मैन्युअल रूप से रद्द करना। – RelativeGames

+0

@clay मैं – Syler

3

एमएसडीएन मंचों पर इसके लिए एक उत्तर है जो आपकी मदद कर सकता है।

http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/c2f5ed68-aac7-42d3-bd59-dbf2673dd89b

मैं एक ऐसी ही समस्या आ रही है, लेकिन मेरी showAsync कॉल अलग धागे पर अलग कार्यों में कर रहे हैं तो मैं एक किया() वहाँ में मुझे नहीं लगता कि नहीं छोड़ सकते हैं ...

+1

एक आवृत्ति चर का उपयोग करने का प्रयास करें और asyncCommand का संदर्भ रखें और जांचें कि आदेश शून्य नहीं है या नहीं। यह काम हो सकता है। – Syler

+1

हाँ जो मेरे मामले में काम करता है। –

3

मुझे कुछ दिन पहले इसी मुद्दे का सामना करना पड़ रहा था, और मैं इसे ShowAsync का इंतजार कर रहा हूं और फिर रिकर्सिव कॉल कर रहा हूं जो संदेशडिअलॉग को फिर से खोलता है।

public async void ShowDlg(){ 
    Action cmdAction = null; 
    var msgDlg = new MessageDialog("Content.", "Title"); 
    msgDlg.Commands.Add(new UICommand("Retry", (x) => { 
    cmdAction =() => ShowDlg(); 
    })); 
    msgDlg.Commands.Add(new UICommand("Cancel", (x) => { 
    cmdAction =() => <Action associated with the cancel button>; 
    })); 
    msgDlg.DefaultCommandIndex = 0; 
    msgDlg.CancelCommandIndex = 1; 

    await msgDlg.ShowAsync(); 
    cmdAction.Invoke(); 
} 

इस सहायता की आशा करें!

9

मैं पार्टी के लिए देर से कर रहा हूँ, लेकिन यहाँ एक तरह से जहां संवाद बॉक्स का परिणाम हमेशा इंतजार कर सकते हैं, साथ ही नहीं के रूप में एक साथ बहुत सारे बुला के बारे में चिंता करने की जरूरत:

पहले एक स्थिर परिभाषित परिवर्तनीय और विधि आपके आवेदन में:

private static IAsyncOperation<IUICommand> messageDialogCommand = null; 
public async static Task<bool> ShowDialog(MessageDialog dlg) { 

    // Close the previous one out 
    if (messageDialogCommand != null) { 
     messageDialogCommand.Cancel(); 
     messageDialogCommand = null; 
    } 

    messageDialogCommand = dlg.ShowAsync(); 
    await messageDialogCommand; 
    return true; 
} 

अब, आप किसी भी संवाद बॉक्स में पास कर सकते हैं और हमेशा निष्पादन का इंतजार कर सकते हैं। यही कारण है कि यह शून्य के बजाय एक बूल देता है। आपको गुणकों के बीच टक्कर के बारे में चिंता करने की ज़रूरत नहीं होगी। इस विधि को स्ट्रिंग स्वीकार क्यों नहीं करें? शीर्षक के कारण, और हां/नहीं कमांड हैंडलर जिन्हें आप उपयोग कर रहे विशिष्ट संवाद बॉक्स में असाइन कर सकते हैं।

आह्वान जैसे:

await App.ShowDialog(new MessageDialog("FOO!")); 

या

var dlg = new MessageDialog("FOO?", "BAR?"); 
dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(YesHandler))); 
dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler(NoHandler))); 
await App.ShowDialog(dlg); 
2

एक अन्य समाधान:

private bool _messageShowing = false; 

// ... 

if (!_messageShowing) 
{ 
    _messageShowing = true; 
    var messageDialog = new MessageDialog("Example"); 

    // ... "messageDialog" initialization 

    Task<IUICommand> showMessageTask = messageDialog.ShowAsync().AsTask(); 
    await showMessageTask.ContinueWith((showAsyncResult) => 
     { 
      _messageShowing = false; 
     }); 
} 
+0

Thx हालांकि बड़े पैमाने पर हैकी लगता है! यह एकमात्र समाधान है, जो मेरे लिए काम करता है! –

संबंधित मुद्दे