2016-01-10 53 views
1

我想從外部環境獲取文本。OnShareTargetActivated on Windows10

protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args) 
    { 
     var frame = new Frame(); 
     frame.Navigate(typeof(Pages.WorkPage), args.ShareOperation); 

     Window.Current.Content = new MainPage(frame); 
     Window.Current.Activate(); 
    } 

我的MainPage Construcor:

public MainPage(Frame frame) 
    { 
     this.InitializeComponent(); 
     this.splitView.Content = frame; 

     ((Frame)splitView.Content).Navigate(typeof(Pages.WorkPage)); 
    } 

..和OnNavigatedTo在我WorkPage這樣

protected async override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     if (e.Parameter == null) return; 

     var shareOperation = (Windows.ApplicationModel.DataTransfer.ShareTarget.ShareOperation)e.Parameter; 
     if (shareOperation.Data.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.Text)) 
      txtMessages.Text = await shareOperation.Data.GetTextAsync(); 
    } 

我怎樣才能改變OnShareTargetActivated讓我OnNavigatedTo正常工作?

回答

2

您正在嘗試加載MainPage並導航其frameWorkPage,同時將共享文本傳遞給WorkPage。爲此,您不需要將新的Frame傳遞給MainPagePage.OnNavigatedTo方法將在頁面加載併成爲父幀的當前源時調用。因此,您可以導航MainPage的幀,並通過OnNavigatedTo方法MainPageOnShareTargetActivated中的共享文本,您可以導航至MainPage

您可以更改OnShareTargetActivated就像這樣:

protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args) 
{ 
    Frame rootFrame = Window.Current.Content as Frame; 
    if (rootFrame == null) 
    { 
     // Create a Frame to act as the navigation context and navigate to the first page 
     rootFrame = new Frame(); 

     // Place the frame in the current Window 
     Window.Current.Content = rootFrame; 
    } 
    rootFrame.Navigate(typeof(MainPage), args.ShareOperation); 
    Window.Current.Activate(); 
} 

MainPage Construcor這樣的:

public MainPage() 
{ 
    this.InitializeComponent(); 

    //this.splitView.Content = frame; 

    //((Frame)splitView.Content).Navigate(typeof(WorkPage)); 
} 

添加的MainPageOnNavigatedTo這樣的:

protected async override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    if (e.Parameter is Windows.ApplicationModel.DataTransfer.ShareTarget.ShareOperation) 
    { 
     var shareOperation = (Windows.ApplicationModel.DataTransfer.ShareTarget.ShareOperation)e.Parameter; 
     if (shareOperation.Data.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.Text)) 
     { 
      var data = await shareOperation.Data.GetTextAsync(); 
      ((Frame)splitView.Content).Navigate(typeof(WorkPage), data); 
     } 
    } 
    else 
    { 
     ((Frame)splitView.Content).Navigate(typeof(WorkPage)); 
    } 
} 

更改OnNavigatedToWorkPage這樣的:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    if (e.Parameter != null) 
    { 
     var data = e.Parameter; 
     txtMessages.Text = data.ToString(); 
    } 
} 

順便說一句,你爲什麼加入了Windows Phone 8.1的標籤這個問題? SplitView在WP8.1中不存在。