从我的UserControl访问MainPage

时间:2016-04-07 09:00:54

标签: c# xaml user-controls

在MyUserControl.xaml.cs

 private void StartVideos()
        {

                    foreach (Uri uri in _videos)
                        _playlist.Enqueue(uri);

                    fv.Visibility = Visibility.Collapsed;

                    fvWelcome.Visibility = Visibility.Collapsed;



                    mediaPlayer.Visibility = Visibility.Visible;

                    mediaPlayer.Source = _playlist.Dequeue();

                    mediaPlayer.Play();

          }
MainPage.xaml中的

 <RelativePanel x:Name="WelcomeRelativePanel" Background="#FF1F4E79" >
            <TextBox x:Name="WelcomeText"
                                 RelativePanel.AlignLeftWithPanel="True"
                                 Margin="145,0,0,0"
                                 Foreground="White"
                                 FontFamily="Segoe UI"
                                 IsReadOnly="True"
                                 BorderBrush="#FF1F4E79" 
                                 BorderThickness="0" 
                                 HorizontalAlignment="Center" FontSize="84"
                                 TextWrapping="Wrap" AcceptsReturn="True" 
                                 Background="#FF1F4E79" 

                                />

如何从UserControl与我的MainPage进行交互 像这样

private void StartVideos()
 {

    .....
    WelcomeRelativePanel.Visibility =  Visibility.Collapsed;
 }

我知道在App.Xaml.cs中使用静态变量 任何人都可以提供样品或其他想法吗?

1 个答案:

答案 0 :(得分:0)

我有这样的问题,但我需要从用户控件中更改TextBlock文本。

<Page
  x:Class="MyProject.MainPage">
  <Grid>
    <TextBlock x:Name="myTextBlock" Text="" />
    <MyUserControl x:Name = "userControl" />
  </Grid>
</Page>

我做了类似这样的事情,并且有效。

在MyUserControl中

private string myString;
public delegate void MyEventHandler(string a);
public event MyEventHandler MyPropertyChanged;

public MyUserControl()
{
    this.MyPropertyChanged+= new MyEventHandler(HandlePropertyChanging);
}

private void HandleCreditsChanging(string a)
{
    a = myString;
}

在MainPage中:

public MainPage()
{
    myUserControl.MyPropertyChanged += MyUserControl_MyPropertyChanged;
}

private void MyUserControl_MyPropertyChanged(string a)
{
    myTextBlock.Text = a;
}

我相信它也适合你。