gpt4 book ai didi

c# - 如何从实现页面访问自定义控件中存在的按钮?

转载 作者:太空宇宙 更新时间:2023-11-03 11:48:38 25 4
gpt4 key购买 nike

我的 generic.xaml 包含以下代码:

<ControlTemplate TargetType="local:customVideoControl">

<Grid>

<Grid.RowDefinitions>
<RowDefinition Height="600"/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>

<MediaElement x:Name="customMediaPlayer" Source="{TemplateBinding CustomMediaSource}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}"
Grid.Row="0" Grid.ColumnSpan="3"
/>

<ToggleButton x:Name="playPauseBtn" Height="50" Width="50" Content="Pause" Grid.Row="1" Grid.Column="0"/>
<Button x:Name="prevBtn" Height="50" Width="50" Content="Prev" Grid.Row="1" Grid.Column="1"/>
<Button x:Name="nextBtn" Height="50" Width="50" Content="Next" Grid.Row="1" Grid.Column="2"/>

</Grid>
</ControlTemplate>

现在在 applyTemplate 上,我正在访问如下控件:

 public override void OnApplyTemplate()
{
base.OnApplyTemplate();

ToggleButton playPauseBtn = GetTemplateChild("playPauseBtn") as ToggleButton;

Button prevBtn= GetTemplateChild("prevBtn") as Button;
Button nextBtn = GetTemplateChild("nextBtn") as Button;
MediaElement customMediaPlayer = GetTemplateChild("customMediaPlayer") as MediaElement;

playPauseBtn.Checked += (obj, Args) =>
{
customMediaPlayer.Pause();
playPauseBtn.Content = "Play";
};

playPauseBtn.Unchecked += (obj, Args) =>
{
customMediaPlayer.Play();
playPauseBtn.Content = "Pause";
};



nextBtn.Click += (obj, Args) =>
{
customMediaPlayer.Source=new Uri(CustomMediaSource.ToString(),UriKind.RelativeOrAbsolute);

};

prevBtn.Click += (obj, Args) =>
{
customMediaPlayer.Source = new Uri(CustomMediaSource.ToString(), UriKind.RelativeOrAbsolute);
};

}

现在我想在我正在实现的页面中访问 nextBtn

CustomVideoControl myVControl=new CustomVideoControl();

这将创建控件的实例,但我想在单击下一个和上一个按钮,thta 存在于 generic.xaml 中的 CustomVideoControl 中。任何帮助将不胜感激。

谢谢,子轩

最佳答案

您只需向您的控件添加几个事件。

 public event EventHandler MovedPrevious
public event EventHandler MovedNext

现在这通常是这样实现的:-

 protected virtual void OnMovedPrevious(EventArgs e)
{
var handler = MovedPrevious;
if (handler != null)
handler(this, e);
}

protected virtual void OnMovedNext(EventArgs e)
{
var handler = MovedNext;
if (handler != null)
handler(this, e);
}

现在在您现有的点击事件中:-

nextBtn.Click += (obj, Args) =>
{
customMediaPlayer.Source=new Uri(CustomMediaSource.ToString(),UriKind.RelativeOrAbsolute); //No idea what this doing
OnMovedNext(EventArgs.Empty);
};

prevBtn.Click += (obj, Args) =>
{
customMediaPlayer.Source = new Uri(CustomMediaSource.ToString(), UriKind.RelativeOrAbsolute); //No idea what this is doing either
OnMovedPrevious(EventArgs.Empty);
};

现在在你的消费代码中你可以做这样的事情:-

CustomVideoControl myVControl=new CustomVideoControl();
myVControl.MovedNext += (s, args) => { /* deal with next */ };
myVControl.MovedPrevious += (s, args) => { /* deal with previous */ };

关于c# - 如何从实现页面访问自定义控件中存在的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2690177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com