I am working with a SettingsPane
that holds settings, and I have all my problems solved - except for one thing I apparently have no control over.
This is the relevant SettingsPane.xaml:
<ComboBox Name="Themes" SelectionChanged="SettingSelectionChanged">
<ComboBoxItem Content="Theme 1" />
<ComboBoxItem Content="Theme 2" />
<ComboBoxItem Content="Theme 3" />
</ComboBox>
This is my SettingsPane.xaml.cs:
public Settings()
{
this.InitializeComponent();
this.DataContext = MainPage.Data;
Themes.SelectedIndex = 0;
}
private void SettingSelectionChanged(object sender, SelectionChangedEventArgs e)
{
ApplicationData.Current.RoamingSettings.Values["Theme"] = Themes.SelectedIndex;
ApplicationData.Current.SignalDataChanged();
}
I handle the SignalDataChanged()
call in MainPage.xaml.cs:
public static MainPageVM Data = new MainPageVM();
public MainPage()
{
Windows.Storage.ApplicationData.Current.DataChanged += (a, o) =>
{
Data.Theme = (int) Windows.Storage.ApplicationData.Current.RoamingSettings.Values["Theme"];
};
}
Theme
is stored in MainPageVM.cs:
private int _theme = 0;
public int Theme
{
get { return _theme; }
set
{
if (value == _theme) return;
_theme = value;
OnPropertyChanged();
}
}
Now, this is bound like this:
<Grid Background="{Binding Theme, Converter={StaticResource ThemeToBackground}}" Name="MainGrid">
It appears to be working, except for one thing. When it hits the OnPropertyChanged()
call in Theme
's setter, it crashes with this error:
Additional information: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
What can I do about this?
Each window has a different thread associated with it; you should avoid referencing one page's view model from another page if there is data binding involved (you will need an adapter that pushes the changes onto the correct thread). [Edit] Also that particular event is being raised on a thread that isn't associated with any page.
User contributions licensed under CC BY-SA 3.0