【问题标题】:access element component silverlight访问元素组件silverlight
【发布时间】:2013-12-10 00:16:16
【问题描述】:

我正在开发基于 Silverlight 5 on MVVM 模式的播放器。我正在尝试更改代码中的状态按钮。

文件:PlayerPage.xaml
我的播放器组件 xaml:

 <Grid x:Name="LayoutRoot">
    <player:Player x:Name="Player"
                   Playlist="{Binding Playlist}"
                   CurrentPlaylistItem="{Binding CurrentPlaylistItem, Mode=TwoWay}"
                   IsSeekingEnabled="{Binding CanSeek}"
                   AutoLoad="True"
                   AutoPlay="True"
                   MediaEnded="Player_MediaEnded"
                   MediaOpened="Player_MediaOpened"
                   Style="{StaticResource PlayerStyle}"
                   PlaylistVisibility="Disabled"
                   LogLevel="Warning"
                   AutoHideControls="True"
                   AutoHideDelay="0:0:5"
                   AllowDoubleClickToggle="True"
                   RetryDuration="00:01:00"
                   RetryInterval="00:00:10"
                   BufferingTime="00:00:15"
        />

</Grid>

文件:PlayerPage.xaml
组件播放器样式:

<Grid x:Name="ControllerContainer" Height="40" Grid.Row="4"    RenderTransformOrigin="0.5,0.5">
   <Grid.RenderTransform>
       <CompositeTransform />
            </Grid.RenderTransform>
               <Grid.Background>
                   <ImageBrush ImageSource="component/Images/grid_background_player.jpg"
                                    AlignmentY="Top" />
                         </Grid.Background>
                          <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="120" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid x:Name="playercontrols" Grid.Column="0" HorizontalAlignment="Left" Width="90">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Grid Grid.Column="0">
                                    <Button x:Name="TbPlayElement" Click="TbPlayElement_Click"  HorizontalAlignment="Left" Cursor="Hand" >
                                                <Grid>
                                                    <Image Source="component/Resources/play.png" Stretch="Uniform" Height="16" Width="16" />
                                                </Grid>
                                    </Button>

现在在 PlayerPage.cs 后面的代码中,当我按下停止按钮时,我想更改播放按钮的图像。

 private void TbStopElement_Click(object sender, System.Windows.RoutedEventArgs e)
    {

    }

如何在后面的代码中访问播放按钮?

【问题讨论】:

  • 将 Image 的来源绑定到 BitmapImage 类型的属性。

标签: c# silverlight xaml code-behind


【解决方案1】:

附加到Loaded 事件以保存图像的引用:

xaml:

<Image Source="component/Resources/play.png" Stretch="Uniform" Height="16" Width="16"
       Loaded="ImageLoaded" />

代码隐藏:

private Image imageReference;
private void ImageLoaded(object sender, EventArgs e)
{
    imageReference = sender as Image;
}

那么当你想改变图像时:

private void TbStopElement_Click(object sender, System.Windows.RoutedEventArgs e)
{
    if(imageReference != null)
    {
        imageReference.Source = new BitmapImage(new Uri("/YourAppName;component/Resources/YourImage.png", UriKind.Relative));
    }
}

【讨论】:

    【解决方案2】:

    像这样访问您的TbStopElement:

            var grid = TbPlayElement.Content as Grid;
            var playImage = grid.Children[0] as Image;
    
            BitmapImage bitmap = new BitmapImage();
            bitmap.UriSource = new Uri("newImage.png", UriKind.Relative);
            playImage.Source = bitmap;
    

    【讨论】:

    • 谢谢,我试试你的代码,bitmap.Beginit 和 bitmap.endInit 不存在,我无法访问 PlayImage x:name。这是我的问题。
    • 对不起,忘了它是WPF 特定的。我会相应地更新。你能不试试这些方法吗?
    • @Flug 当您说您无权访问 PlayImage x:Name 时,您的意思是您无权访问源代码吗?
    • @Flug 现在您无需 x:name 即可访问。我已更改为代码示例。
    【解决方案3】:

    我建议你这个代码

    TbPlayElement.Source = new BitmapImage(new Uri("/TbPlayElement.content;component/NameNewImage.png", UriKind.Relative));
    

    备注:在 Visual Studio 中检查 xaml 文件的属性,确保 Build Action is set to Page。

    或者你可以使用切换按钮

    链接:http://msdn.microsoft.com/en-us/library/cc296245%28v=vs.95%29.aspx

    【讨论】:

    • 我测试了这段代码,但是按钮或图像无法被visual studio识别,我无法访问它的属性,所以我没有编译。
    • 属性是你的图像控件,但是路径,你必须添加图像文件,确保图像在你的存储库中
    • 我不确定我是否理解。我的图片是投注资源,我的问题是我无法获得此按钮或图片
    • 将 playButtonImage 替换为 TbPlayElement(因为它是您的对象类型按钮的 id)
    • 是一样的。我无法访问它。我无法在后面的代码中访问播放器的元素。完整的 xaml 页面:pastebin.com/Tk7JSZCT
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多