2017-07-17 7 views
0

プログラミングに新しい、7日間の検索の後に解決策が見つかりませんでした。 私の "MainPage"が開き、クリックすると、コアDispatcherを持つ新しいスレッドウィンドウで開く "SecondaryPage"が開きます。私の "MainPage"には "TextBlock1"を更新するボタンClickイベントがあります。私が達成できないことは、この値を "SecondPage" "TextBlock2"に渡すことです。UWP C#別のスレッドに変数を渡す

下記のMainPage.xaml。

<StackPanel> 
    <Button Click="Button_Click_NewWindow" Content="Start a new window" FontSize="30"/> 
    <TextBlock Name="Textblock1" Text="Empty" FontSize="30"/> 
    <Button Click="Button_Click_Add" Content="+1" FontSize="30"/> 
</StackPanel> 

SecondPage.xaml

の背後にあるコード
<StackPanel> 
    <TextBlock Name="Textblock2" Text="Empty" FontSize="30"/> 
</StackPanel> 

MainPage.xaml.cs私はDispatcherは私のメインページの表示に取り組んでいるが、私は指示するコードを見つけ出すカント参照

namespace MulitViewV1 
{ 

    public sealed partial class MainPage : Page 
    { 
     public int T1G = 0; 


     public MainPage() 
     { 
      this.InitializeComponent(); 
     } 

     private async void Button_Click_NewWindow(object sender, RoutedEventArgs e) 
     { 
      CoreApplicationView newView = CoreApplication.CreateNewView(); 
      int newViewId = 0; 
      await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
      { 
       Frame frame = new Frame(); 
       frame.Navigate(typeof(SecondPage), null); 
       Window.Current.Content = frame; 
       // You have to activate the window in order to show it later. 
       Window.Current.Activate(); 

       newViewId = ApplicationView.GetForCurrentView().Id; 
      }); 
      bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId); 
     } 

     private async void Button_Click_Add(object sender, RoutedEventArgs e) 
     { 
      T1G = T1G + 1; 
      Textblock1.Text = T1G.ToString(); 

      await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() => 
      { 
       //UI code here 
       // NOT WORKING error "TextBlock2" does not exist in the current context 
       Textblock2.Text = T1G.ToString(); 
      }); 

     } 
    } 
} 

SecondPage "newViewID"に送信します。あなたは

string TextBoxText = Textblock1.Text; 
frame.Navigate(typeof(SecondPage), TextBoxText); 

そして、あなたのSecondPageで以下のようにTextBlockからテキストを渡し、

frame.Navigate(typeof(SecondPage), null); 

代わりにヌルを呼び出しているあなたのButton_Click_NewWindow

答えて

0

、メソッドにOnNavigatedをオーバーライドして、あなたが得ることができます以下のようなテキスト。

string PassedText=e.parameter.ToString(); 

、あなたは

は、以下にごButton_Click_NewWindowを変更し、上に2つの変数を追加、更新

Textblock2.Text=PassedText; 

次のようにあなたのTextBlockにこれを割り当てることができます。

int newViewId = 0; 
Window SecondWindow; 
private async void Button_Click_NewWindow(object sender, TappedRoutedEventArgs e) 
{ 
    string TextBoxText = Textblock1.Text; 
    int mainViewId = ApplicationView.GetApplicationViewIdForWindow(CoreApplication.MainView.CoreWindow); 
    if (newViewId == 0) 
    { 
     CoreApplicationView newCoreView = CoreApplication.CreateNewView(); 
     ApplicationView newAppView = null; 
     await newCoreView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
     { 
      newAppView = ApplicationView.GetForCurrentView(); 
      Window.Current.Content = new Frame(); 
      (Window.Current.Content as Frame).Navigate(typeof(SecondPage), TextBoxText); 
      Window.Current.Activate(); 
      SecondWindow = Window.Current; 
     }); 
     newViewId = newAppView.Id; 
    } 
    else 
    { 
     await SecondWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
     { 
      SecondWindow.Content = new Frame(); 
      (SecondWindow.Content as Frame).Navigate(typeof(SecondPage), TextBoxText); 
      SecondWindow.Activate(); 
     }); 

    } 
    await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId, ViewSizePreference.UseHalf, mainViewId, ViewSizePreference.UseHalf); 
} 

そして、あなたの出力

enter image description here

+0

私と一緒にお越しいただきありがとうございます。私のButton_Click_NewWindowでは、2番目のウィンドウが開きます。コード::frame.Navigate(typeof(SecondPage)、T1G);変数を渡します。次にButton_Click_Add、新しい更新変数を2番目に開いたウィンドウに渡すにはどうすればいいですか? – Static

+0

@Static私の更新された答えを確認 – AVK

+0

私は十分にありがとう、私はこれを把握しようと1週間立ち往生してきた。 – Static

1

すべての時間をDispatcherコールする必要はありません。ハンドラもUIスレッド上に置かれるように、ボタンのクリックはUIスレッド上で行われます。

考えてみるには、*Asyncで何かをしていない限り、あなたは常にUIスレッドを使用しているということです。あなたが非同期のものを待っているなら、あなたは良いです。これは常に真実ではありませんが、あなたが遭遇する可能性のある問題を開始して処理する場所です。

通常、アプリケーションにはFrameが1つだけ必要です。複数のFrames(および複数のウィンドウ)は高度なトピックです。後で保存してください。

デフォルトでは、UWPアプリにはFrameが含まれます。ページはこのフレームでホストされます。フレームは、ホストするページ間を移動できます。フレームはブラウザウィンドウと考えると、ページはウェブページに似ています。

Frameは、ページ上にFrameというプロパティが表示されます。新しいページに移動するには、次の操作を行います。

private async void Button_Click_NewWindow(object sender, RoutedEventArgs e) 
{ 
    Frame.Navigate(typeof(SecondPage), null); 
} 

次はあなたがTextBlock2のテキストを変更したいです。 TextBlock2SecondPageにありますが、あなたのコードはのコードであり、TextBlock2はありません。TextBlock2MainPageのものではありません。

あなたのSecondPage.xaml.csの中には、TextBlock2があるのでそこに入ることができます。ただし、MainPageは、SecondPageへのナビゲーション後には表示されません(1つから別のナビゲーションに同時に2つのウェブページが表示されないのと同じように)。

関連する問題