1

私は c# を学んでいて、よくわからない WPF を使用するプロジェクトに取り組むように依頼されました。MUIも使用しています。私はかなり基本的なタスクを達成しようとしています。と という 2 つのページがClientRNG.xamlありServerRNG.xamlます。I have two buttons and two textfieldではClientRNG.xaml、各ボタンが押されると乱数が生成され、テキスト ボックスに表示されます。にはServerRNG、上記と同じ機能を持つボタンとテキストフィールドが 1 つだけあります。したがって、3 つの異なる乱数 ( に 1 つ、ServerRNG.xamlに 2 つ) が得られClientRNGます。私がやりたいことは、これらの乱数を という別のページに渡すことですSSL.xaml

ページは次の場所で作成され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>

クライアントRNG

<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>

今、私はオムリビタンが投稿したソリューションを使用しようとしていましたが、まだ苦労しています

4

2 に答える 2

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 コンテナを使用して の現在のインスタンスを解決しますSplitPage1
  2. あなたのコードによると、これらのページを作成している第三者がいるようです。それが本当なら、後で使用できる作成中ServerRNGの参照を渡すSplitPage1ことができます。たとえば、次のようになります。

    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_1onで、ServerNRG値を更新してイベントを発生させます

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

お役に立てれば

于 2013-10-20T07:16:55.330 に答える
0

MVVM デザイン パターンを使用して WPF で作業する必要があります。MVVM フレームワークがないとちょっと面倒です。http://caliburnmicro.codeplex.com/を使用することをお勧めします。

このチュートリアルを使用して開始します

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

また、メディエーター パターン (caliburn micros イベント アガレーターを使用) を使用して、異なるウィンドウ間で値/コマンドを渡す方法についても説明します。

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

于 2013-10-20T08:38:18.527 に答える