2013-10-20 66 views
1

我正在學習c#,並且我被要求使用WPF進行項目工作,但我不太清楚。我們也使用MUI。 我正在嘗試實現一個非常基本的任務。我有兩頁叫ClientRNG.xamlServerRNG.xaml。在ClientRNG.xaml我有兩個按鈕和兩個文本框,當每個按鈕被按下時,一個隨機數字被生成並出現在一個文本框中。在ServerRNG中只有一個按鈕和一個文本框,具有與上述相同的功能。 所以我最終會得到三個不同的隨機數,一個在ServerRNG.xaml和兩個在ClientRNG。 我想要做的是將這些隨機數字傳遞到另一個頁面,稱爲SSL.xaml傳遞兩個頁面之間的值wpf

的頁面在MainWindow.xml創建:

<mui:ModernWindow.MenuLinkGroups> 
    <mui:LinkGroup DisplayName="network security"> 
     <mui:LinkGroup.Links> 
      <mui:Link DisplayName="Home" Source="/Pages/Home.xaml" /> 
      <mui:Link DisplayName="RNG" Source="/Pages/ClientRNG.xaml" /> 
      <mui:Link DisplayName="3DES" Source="/Pages/3des.xaml" /> 
      <mui:Link DisplayName="RSA" Source="/Pages/RSA.xaml" /> 
      <mui:Link DisplayName="SHA-1" Source="/Pages/sha1.xaml" /> 
      <mui:Link DisplayName="PKI Certificates" Source="/Pages/pki.xaml" /> 
      <mui:Link DisplayName="SSL" Source="/Pages/SSL.xaml" /> 

     </mui:LinkGroup.Links> 
    </mui:LinkGroup> 
    <mui:LinkGroup DisplayName="settings" GroupName="settings"> 
     <mui:LinkGroup.Links> 
      <mui:Link DisplayName="software" Source="/Pages/Settings.xaml" /> 
     </mui:LinkGroup.Links> 
    </mui:LinkGroup> 
</mui:ModernWindow.MenuLinkGroups> 

<mui:ModernWindow.TitleLinks> 
    <mui:Link DisplayName="settings" Source="/Pages/Settings.xaml" /> 
</mui:ModernWindow.TitleLinks> 

守則ClientRNG:

namespace NetworkSecuritySSLTest.Pages 
{ 
/// <summary> 
/// Interaction logic for RNG.xaml 
/// </summary> 
public partial class ClientRNG : UserControl 
{ 
    public ClientRNG() 
    { 
     InitializeComponent(); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     Random r = new Random(1); 
     int number = r.Next(0, 100); 
     r1Out.Text = number.ToString(); 
     SharingManager.GlobalValue = number; 
    } 

    private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     Random r = new Random(3); 
     int number = r.Next(0, 100); 
     pmsOutC.Text = number.ToString(); 
    } 

這裏是我在ServerRNG代碼:

namespace NetworkSecuritySSLTest.Pages 
{ 
/// <summary> 
/// Interaction logic for RNG.xaml 
/// </summary> 
public partial class ServerRNG : UserControl 
{ 

    private SplitPage1 sp; 



    public ServerRNG() 
    { 

     InitializeComponent(); 

    } 

    private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     Random r = new Random(2); 
     int number = r.Next(0, 100); 
     r2Out.Text = number.ToString(); 
     SharingManager.GlobalValue = number; 
    } 
} 

}

,這是背後SSL類的代碼

namespace NetworkSecuritySSLTest.Pages 
{ 
/// <summary> 
/// Interaction logic for SplitPage1.xaml 
/// </summary> 
public partial class SplitPage1 : UserControl 
{ 
    private int r1FromClient; 
    public SplitPage1() 
    { 
     InitializeComponent(); 
     SharingManager.ValueChanged += UpdateTextBox1; 
     SharingManager.ValueChanged += UpdateTextBox2; 

    } 

    public void UpdateTextBox1(object sender, NumericEventArgs e) 
    { 
     r1SSLBox.Text = e.Value.ToString(); // Update textBox 
    } 

    public void UpdateTextBox2(object sender, NumericEventArgs e) 
    { 
     r2SSLBox.Text = e.Value.ToString(); // Update textBox 
    } 

} 

} 這裏是XAML: 'SplitPage1'

<UserControl x:Class="NetworkSecuritySSLTest.Pages.SplitPage1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mui="http://firstfloorsoftware.com/ModernUI" 
     mc:Ignorable="d" 
     d:DesignWidth="766.507" Height="535"> 
<Grid Style="{StaticResource ContentRoot}"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="6"/> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 

    <ScrollViewer> 
     <StackPanel> 
      <TextBlock Text="CLIENT" Style="{StaticResource Heading2}" /> 
      <TextBlock x:Name="hello" Text="Hello Server. This is my Random Number and my Security Capabilities:" FontSize="12" FontStyle="Italic" Margin="0,10,0,0" UseLayoutRounding="False" TextWrapping="Wrap" /> 
      <TextBlock x:Name="helloCont" Text="" FontSize="12" FontStyle="Italic" Margin="0,0,0,0" /> 
      <TextBox x:Name ="r1SSLBox" HorizontalAlignment="Left" Height="57" Margin="10,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.498,0.404"/> 
      <TextBox x:Name ="r2SSLBox" HorizontalAlignment="Left" Height="57" Margin="10,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.498,0.404"/> 
      <TextBlock x:Name="VerifyDC" Text="I need to verify your Digital Certificate:" FontSize="12" FontStyle="Italic" Margin="0,10,0,0" /> 
      <TextBlock x:Name="VerifyCont" Text="" FontSize="12" FontStyle="Italic" /> 

      <TextBlock x:Name="MSK" Text="My Master Key is:" FontSize="12" FontStyle="Italic" Margin="0,10,0,0" /> 
      <TextBlock x:Name="MSKCont" Text="" FontSize="12" FontStyle="Italic" /> 

     </StackPanel> 
    </ScrollViewer> 
    <GridSplitter Grid.Column="1" /> 
    <ScrollViewer Grid.Column="2 " Margin="{StaticResource SplitRight}"> 
     <StackPanel> 
      <TextBlock Text="SERVER" Style="{StaticResource Heading2}" /> 
      <TextBlock Text="Content goes here" /> 
     </StackPanel> 
    </ScrollViewer> 
    <GridSplitter Grid.ColumnSpan="3" HorizontalAlignment="Left" Margin="0,23,0,0" VerticalAlignment="Top" Width="735"/> 
    <Button Content="Man-In-The-Middle-Attack" HorizontalAlignment="Left" VerticalAlignment="Top" Width="209" RenderTransformOrigin="0.055,0.397" Height="40" Margin="255,451,0,0" Grid.ColumnSpan="3" /> 

</Grid> 

ClientRNG

<UserControl x:Class="NetworkSecuritySSLTest.Pages.ClientRNG" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mui="http://firstfloorsoftware.com/ModernUI" 
     mc:Ignorable="d" 
     d:DesignWidth="766.507" Height="535"> 
<Viewbox Stretch="None"> 
    <Grid Style="{StaticResource ContentRoot}" Height="301" Margin="0" Width="435"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition Width="0*"/> 
     </Grid.ColumnDefinitions> 

     <!-- TODO: set @SelectedSource --> 

     <mui:ModernTab Layout="Tab" Margin="0,52,0,0"> 
      <mui:ModernTab.Links> 
       <!-- TODO: set @Source --> 
       <mui:Link DisplayName="Client" /> 
       <mui:Link DisplayName="Server" Source="/Pages/ServerRNG.xaml" /> 
      </mui:ModernTab.Links> 
     </mui:ModernTab> 

     <Button Content="GENERATE R# 1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.055,0.397" Height="26" Margin="10,52,0,0" FontSize="11" Click="Button_Click" /> 
     <TextBox Name ="r1Out" HorizontalAlignment="Left" Height="57" Margin="10,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.498,0.404"/> 

     <Button Content="GENERATE MS" HorizontalAlignment="Left" VerticalAlignment="Top" Width="119" RenderTransformOrigin="0.055,0.397" Height="26" Margin="306,52,0,0" Click="Button_Click_2" /> 
     <TextBox Name ="msOutC" HorizontalAlignment="Left" Height="57" Margin="306,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="119" RenderTransformOrigin="0.498,0.404"/> 

     <Button Content="GENERATE PMS" HorizontalAlignment="Left" VerticalAlignment="Top" Width="133" RenderTransformOrigin="0.055,0.397" Height="26" Margin="151,52,0,0" Click="Button_Click_1" /> 
     <TextBox Name ="pmsOutC" HorizontalAlignment="Left" Height="57" Margin="151,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="133" RenderTransformOrigin="0.498,0.404"/> 


     <Label Content="Random Number Generator" HorizontalAlignment="Left" Height="19" Margin="10,10,0,0" VerticalAlignment="Top" Width="415" FontWeight="Bold"/> 


    </Grid> 

</Viewbox> 

ServerRNG

<UserControl x:Class="NetworkSecuritySSLTest.Pages.ServerRNG" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mui="http://firstfloorsoftware.com/ModernUI" 
     mc:Ignorable="d" 
     d:DesignWidth="766.507" Height="535"> 
<Viewbox Stretch="None"> 
    <Grid Style="{StaticResource ContentRoot}" Height="301" Margin="0" Width="435"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition Width="0*"/> 
     </Grid.ColumnDefinitions> 

     <!-- TODO: set @SelectedSource --> 

     <mui:ModernTab Layout="Tab" Margin="0,52,0,0"> 
      <mui:ModernTab.Links> 
       <!-- TODO: set @Source --> 
       <mui:Link DisplayName="Client" Source="/Pages/ClientRNG.xaml" /> 
       <mui:Link DisplayName="Server" /> 
      </mui:ModernTab.Links> 
     </mui:ModernTab> 

     <Button Name ="r2but" Content="GENERATE R# 2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.055,0.397" Height="26" Margin="76,52,0,0" FontSize="11" Click="Button_Click_1" /> 
     <TextBox Name ="r2Out" HorizontalAlignment="Left" Height="57" Margin="76,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.498,0.404"/> 

     <Button Content="GENERATE MS" HorizontalAlignment="Left" VerticalAlignment="Top" Width="119" RenderTransformOrigin="0.055,0.397" Height="26" Margin="249,52,0,0" /> 
     <TextBox Name ="msOutS" HorizontalAlignment="Left" Height="57" Margin="249,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="119" RenderTransformOrigin="0.498,0.404"/> 


     <Label Content="Random Number Generator" HorizontalAlignment="Left" Height="19" Margin="10,10,0,0" VerticalAlignment="Top" Width="415" FontWeight="Bold"/> 


    </Grid> 

</Viewbox> 

現在我試圖用張貼Omribitan的解決方案,但我仍然在努力

+0

你是如何顯示**'SplitPage1'?在你發佈的代碼中,你只是在實例化它... –

+0

你將不得不分享xaml來告訴兩個用戶控件之間的關係。他們是兄弟姐妹還是有親子關係? – Nitin

+0

@nit他們是兄弟姐妹,對不起,我沒有編碼的xaml別人做了它 – mikey

回答

1

根據你在說已經顯示SplitPage1的評論欄,

你在做你的代碼是什麼創造了SplitPage1一個實例,它傳遞數據

SplitPage1 sp = new SplitPage1(); // This is a new page, not the one currently shown in your application 
    sp.Setr1SSLBox(number); // it should set the text box in SSL page 

所以,如果你想設置的當前顯示SplitPage1文本,你需要得到它是參考。這很難說,因爲怎麼也看不出你的整個代碼,但是這是我會考慮:

  1. 使用IoC container解決的SplitPage1當前實例。
  2. 根據你的代碼好像有一個第三方創建這些頁面。如果這是真的,它可以通過ServerRNGSplitPage1它的創建,你就可以在以後使用,例如參考:

    public partial class ServerRNG : UserControl 
    { 
        private SplitPage1 sp; 
    
        public ServerRNG(SplitPage1 splitPage) : this() 
        { 
          sp = splitPage; // Save a reference to the currently displayed `SplitPage1` page 
        } 
    
        public ServerRNG() 
        { 
          InitializeComponent(); 
        } 
    
        private void Button_Click_1(object sender, RoutedEventArgs e) 
        { 
          Random r = new Random(2); 
          int number = r.Next(0, 100); 
          r2Out.Text = number.ToString(); 
          sp.Setr1SSLBox(number); // Set the correct instance's text 
        } 
    } 
    
  3. 創建一個類將公開一個靜態屬性和事件,將觸發當屬性更改:

    public class SharingManager 
    { 
        // Define a global static event to be fired when the value is changing 
        public static event EventHandler<NumericEventArgs> ValueChanged; 
    
        public static int GlobalValue 
        { 
         set 
         { 
          // Fire ValueChanged event 
          if (ValueChanged != null) 
           ValueChanged(null, new NumericEventArgs(value)); 
         } 
        } 
        } 
    
    public class NumericEventArgs : EventArgs 
    { 
        public NumericEventArgs(int value) 
        { 
         Value = value; 
        } 
    
        public int Value { get; set; } 
    } 
    

SplitPage1

public SplitPage1() 
    { 
     InitializeComponent(); 
     SharingManager.ValueChanged += UpdateTextBox; 

    } 

public void UpdateTextBox(object sender, NumericEventArgs e) 
{ 
    r1SSLBox.Text = e.Value.ToString(); // Update textBox 
} 
註冊一個處理程序

Button_Click_1ServerNRG,更新值觸發事件

Random r = new Random(2); 
    int number = r.Next(0, 100); 
    r2Out.Text = number.ToString(); 
    SharingManager.GlobalValue = number; 

希望這有助於

+0

我之前有類似的解決方案,但是我的和你的都得到這個錯誤:「對象引用沒有設置爲對象的一個​​實例」。它指的是'sp'變量...我必須在某處調用ServerRNG(SplitPage1 splitPage)嗎? – mikey

+0

我忘記提及我們正在使用'mui' ...我不知道這是否有幫助是導致splitPage1 – mikey

+0

@lucaConfa是的,當然,就像我說的,如果有第三方創建這個類,它將不得不使用ServerRNG(SplitPage1 splitPage)構造函數來創建'ServerRNG',而不是它的默認構造函數。 –

0

你應該WPF使用MVVM設計模式的工作原理。 它有點兒討厭不MVVM框架 我建議你使用http://caliburnmicro.codeplex.com/

使用本教程上手

http://www.mindscapehq.com/blog/index.php/2012/01/12/caliburn-micro-part-1-getting-started/

這也解釋瞭如何使用調解模式(使用卡利萬分之一事件aggerator)至在不同的窗口之間傳遞值/命令。

http://www.mindscapehq.com/blog/index.php/2012/2/1/caliburn-micro-part-4-the-event-aggregator/