【问题标题】:Silverlight reference XAML from codeSilverlight 从代码中引用 XAML
【发布时间】:2012-10-17 12:12:21
【问题描述】:

在 Silverlight5 中,如何从 button_click 事件中引用用户控件资源 - 表情?

如果我在后面的代码中构建它然后以这种方式进行数据绑定,我可以很好地访问该对象,但是使用 XAML 方法我得到了一些更好的工具。例子来了fromCh16

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            // **HERE how do I reference the XAML emoticon object

            //emoticon.Name = "Smiley face2";
            //emoticon.Icon = new BitmapImage(new Uri("icons/happy.png", UriKind.RelativeOrAbsolute));
        }

.

<UserControl x:Class="EmoticonExample.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:EmoticonExample"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="400">
    <UserControl.Resources>
        <local:Emoticon x:Key="emoticon"
                        Name="SmileyFace"
                        Icon="icons/happy.png" />
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot"
          Margin="10"
          Background="White"
          DataContext="{StaticResource emoticon}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBlock Text="Name:" />
        <TextBlock Text="Image:" Grid.Column="1" />

        <TextBox Name="myTextBox" Text="{Binding Name, Mode=TwoWay}" Grid.Row="1" />
        <Image Name="myImage" Source="{Binding Icon}" Stretch="None" Grid.Row="1" Grid.Column="1" />

        <Button Content="Button" HorizontalAlignment="Left" Margin="10,116,0,-92" Grid.Row="1" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
    </Grid>

</UserControl>

【问题讨论】:

    标签: silverlight xaml


    【解决方案1】:

    为您的用户控件命名

    <UserControl x:Class="EmoticonExample.MainPage" 
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:local="clr-namespace:EmoticonExample" 
                 mc:Ignorable="d" 
                 x:Name="Example"
                 d:DesignHeight="300" 
                 d:DesignWidth="400"> 
    
    </UserControl
    

    然后在后面的代码中引用它:

    var emoticon = Example.Resources["emoticon"] as Emoticon; // Im not sure of your data type
    

    【讨论】:

      猜你喜欢
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      相关资源
      最近更新 更多