2017-01-06 4 views
0

Я использую управление клавиатуры от here.Заменить ресурс во время выполнения

ресурсы определяются в 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 без успеха.

+0

У вас есть 2 findresources, приложение и проверку элемента управления, который вы используете ... – Johnny

ответ

1

Вы можете получить доступ к ресурсу в 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); 
}