2012-11-26 36 views
3

因此,我一直在環顧四周,並沒有找到解決方案,使用WPF Window s。發送數據在WPF窗口

所以我想要做的是將兩個滑塊的值從一個WPF Window發送到另一個。在同一個項目中如何使用兩個WPF Window來完成?是否有可能從同一個解決方案中的另一個項目中使用引用來完成它?

我一直在使用Windows窗體,可以很容易地在那裏工作,但它不會與WPF Window s一起工作。

[編輯]真正需要做的是非常基礎的。我們有一個名爲Startup的菜單項目,其中MainWindow.xaml尚未包含任何需要提及的代碼。雖然我們希望從菜單中選擇2個滑塊,1可以設置要參與的玩家數量,另一個可以設置遊戲應該持續的小時數。我們已經從遊戲本身設定的玩家數量是這樣固定的這個問題:

private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) // Change move-army-strenght 
{ 
    movearmy = Convert.ToInt32(slider1.Value); 
    lblresult3.Content = "" + movearmy; 
    if (menuphase == 1) { players = Convert.ToInt32(slider1.Value); } 
} 

這將檢查它是否是一個menuphase然後得到的來自玩家的數量。雖然,我們想從Menu發送此值,而不是在遊戲中!

+1

請提供一些更多的信息。您在Winforms應用程序中使用什麼代碼?如果我們可以看到一些代碼,我們可以確定它爲什麼不適用於WPF。 –

+1

有很多路要走。如果您願意分享目標,我們可以幫您更輕鬆地選擇合適的目標。 :)編輯:按'目標',我的意思是實際使用案例 – XamlZealot

+0

更新!在文本中添加[編輯] – JakobMillah

回答

2

對於您的需求來說這可能是過度的,但在窗體,窗口等之間發送帶有數據的消息的可擴展方式是使用Mediator Design Pattern。 Marlon Grech有一篇很好的文章和樣本項目,文章編號爲WPF Mediator

這種模式適用於發送各種消息,只要沒有太多的訂閱者在尋找消息。

-1

的OP在編輯寫道:

[解決方法]

public partial class SelectionWindow : Window 
{ 
    WpfApplication1.GameWindow newForm2; 

    public SelectionWindow() 
    { InitializeComponent(); } 

    private void continue_Click(object sender, RoutedEventArgs e) 
    { 
     if (Convert.ToInt32(slider1.Value) > 1) 
     { 
      Worldmap.Properties.Settings.Default.value1 = Convert.ToInt32(slider1.Value); 
      Worldmap.Properties.Settings.Default.value2 = Convert.ToInt32(slider2.Value); 
      Worldmap.Properties.Settings.Default.Save(); 
      newForm2 = new WpfApplication1.GameWindow(); 
      newForm2.Show(); 
      Close(); 
     } 
    } 

    private void slider1_ValueChanged_1(object sender, RoutedPropertyChangedEventArgs<double> e) 
    { playerresult.Content = Convert.ToInt32(slider1.Value); } 

    private void slider2_ValueChanged_1(object sender, RoutedPropertyChangedEventArgs<double> e) 
    { timeresult.Content = Convert.ToInt32(slider2.Value); } 
} 
+0

([在編輯中回答並轉換爲社區wiki](http://meta.stackoverflow.com/questions/267434/what-is-the-woole-action-when-the-answer-to-a-question -is添加到所述闕))。 –