uwp multi views update GridView

0

Thanks in advance.

as I know, The first view that’s created when your app starts is called the main view, and Other views, including all views that you create by calling CreateNewView in your app code, are secondary views.

I also know, I can't update UI in thread if they are NOT UI Thread.

I've searched for a long time on net, but haven't found anything. Please help and try to give some ideas how to achieve this.

Here is my code, has been simplified for simplicity:

If I load Items in Page when it start:

public sealed partial class MainPage : Page
{
    private ObservableCollection<Item> Items;//a GridView bind in this.

    public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;

            Items=  new ObservableCollection<Item>();

            Update();
        }
    async void Update()
    {
        //such as I have a lot of Items
        foreach(Item i in Itemlist)
        {
            Items.add(i);
        }
    }
    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        CoreApplicationView newView = CoreApplication.CreateNewView();
        int newViewId = 0;
        await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            Frame frame = new Frame();
            frame.Navigate(typeof(MainPage), null);   
            Window.Current.Content = frame;
            // You have to activate the window in order to show it later.
            Window.Current.Activate();

            newViewId = ApplicationView.GetForCurrentView().Id;
        });
        bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
    }
}

Here is Problem:

  1. as we know, ObservableCollection<T> have NotifyCollectionChangedEventHandler, when data changed, it will Notify UI thread.

  2. But secondary views(a New MainPage), when it run, UpDate() method change the Items list, then error(a GridView Bind in this).

the application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))" at Windows.UI.Xaml.Controls.Image.put_Source(ImageSource value) at APP1.MainPage.XamlBindingSetters.Set_Windows_UI_Xaml_Controls_Image_Source(Image obj, ImageSource value, String targetNullValue) at APP1.MainPage.MainPage_obj2_Bindings.Update_image(BitmapImage obj, Int32 phase) at APP1.MainPage.MainPage_obj2_Bindings.Update_(FloderItem obj, Int32 phase) at APP1.MainPage.MainPa

my Opinion is the data changed, and The ObservableCollection invoke a Event to update secondary view's UI, but not in UI thread.

I think if NotifyCollectionChangedEvent in UI thread, it be fine.

anybody have ideas? thanks a lot.

c#
multithreading
uwp
asked on Stack Overflow Dec 4, 2017 by haihan jin • edited Dec 4, 2017 by A. Milto

1 Answer

1

I did find the good way to solve this problem.

as I said, I can't update UI in thread if they are NOT UI Thread, but my problem is not this.

In MainPage, I use a static object in MainPage class.......then I create a new View, the code also try to use this static obj.

cancel the static flag, I solve this problem.

answered on Stack Overflow Dec 5, 2017 by haihan jin

User contributions licensed under CC BY-SA 3.0