在运行时替换资源

丹尼尔

我从这里开始使用键盘控件

资源在xaml中定义:

   <Grid.Resources>
            <ResourceDictionary x:Name="resdictionary">
                <!-- Img sources-->
                <ImageSource x:Key="EngRus">/TermControls;component/Images/Eng-Rus.png</ImageSource>
                <ImageSource x:Key="gEngRus">/TermControls;component/Images/gEng-Rus.png</ImageSource>
 ...

如何用运行时加载的图像替换它们?我玩了findresources没有成功。

毫米8

您可以通过其关键字访问ResourceDictionary中的资源:

public OnScreenKeyboard()
{
    this.InitializeComponent();
    System.Windows.Media.ImageSource EngRus = MainGrid.Resources["EngRus"] as System.Windows.Media.ImageSource;
}

...然后将其替换为具有相同密钥的另一个资源:

public OnScreenKeyboard()
{
    this.InitializeComponent();
    //remove the old resource
    MainGrid.Resources.Remove("EngRus");

    //create a new BitmapImage
    System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage(new System.Uri("/TermControls;component/Images/shift.png", System.UriKind.RelativeOrAbsolute));
    MainGrid.Resources.Add("EngRus", image);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章