Dear all:
I am trying to development a windows 8.1 store application using C# and VS 2013 that has two screen scenarios
where I publish one string type object from one view to another view using IEventAggregator(Prism), while assigning(data binding) that object to a property of
same type in subscriber class I got below exception:
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.Practices.Prism.StoreApps.DLL
Additional information: Unable to cast COM object of type 'System.ComponentModel.PropertyChangedEventHandler' to class type 'System.ComponentModel.PropertyChangedEventHandler'. Instances of types that represent
COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
public MainPageViewModel(IEventAggregator eventaggregator)
{
var stringEvent = eventaggregator.GetEvent<ShowStringEvent>();
stringEvent.Subscribe(StringReceived);
}
private void StringReceived(string str)
{
Temperature = Convert.ToInt32(str);
}
public class ShowStringEvent : PubSubEvent<string>
{
}
private int _temperature = 10;
public int Temperature
{
get { return this._temperature; }
set
{
if (value != this._temperature)
{
this._temperature = value;
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs("Temperature")); When the second view subscribing
that object and update this property, there will raise a exception
}
}
}
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
int embeddedPageViewId = ApplicationView.GetForCurrentView().Id;
int? mainPageViewId = null;
IEventAggregator eventAggregator = new EventAggregator();
this.DataContext = new EmbeddedPageViewModel(eventAggregator);
var view = CoreApplication.CreateNewView();
await view.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
mainPageViewId = ApplicationView.GetForCurrentView().Id;
//eventAggregator = new EventAggregator();
var mainPageViewModel = new MainPageViewModel(eventAggregator);
var rootFrame = new Frame();
Window.Current.Content = rootFrame;
rootFrame.Navigate(typeof(MainPage),mainPageViewModel);
});
if (mainPageViewId.HasValue)
{
await ProjectionManager.StartProjectingAsync(mainPageViewId.Value, embeddedPageViewId);
}
}