UWP c# Accessing a MainPage Textblock from a class

-1

On a Mainpage XAML I have a CommandBar that I use to display the application main buttons and a message area. It looks a bit like this:

      <CommandBar x:Name="topcmdbar" Grid.Column="0 " Grid.ColumnSpan="3" 
                IsDynamicOverflowEnabled="False" ClosedDisplayMode="Compact" 
                VerticalAlignment="Top" Opacity=".5" 
                Background="Transparent" Visibility="Visible" 
                >
        <CommandBar.Content>
            <Grid >
                <TextBlock  x:Name="TopCmdBarContent" x:FieldModifier="public" HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Stretch"/>
            </Grid>
        </CommandBar.Content>
        <AppBarButton FontFamily="Segoe MDL2 Assets" Content="&#xE838;" FontSize="18"  Label="Open Folder" Tapped="StartNewPlaylist" HorizontalAlignment="Left"/>
        <AppBarButton  etc etc etc........

I have a class that, amongst other things, loads the files for the application. I'd like to display from that class the progress of the loading on the TopCmdBarContent.

I've tried various strategies for this, for e.g. passing the MainPage as an argument to the method that initializes the class, but I cannot access it. I've even thought of creating from within the class its own "progress" message,but then I'd need to access the grid from the mainpage to hang it to....

So, for example, I need to have a method that does something like this within the class:

        public async void InitializePlayList()
        {   
            int countfiles=0;
            StorageFolder f = await GetSelectedFolderAsync();
            foreach (var file in f) 
                 {
                   Do_a_bunch_of_stuff(f);
                   countfiles+=1;
                   TopCmdBarContent="Hang in there, "countfiles.ToString()+" processed";

        }

I have been trying this potential solution but I keep getting this error: "The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E)"

Any idea how I can do this?

c#
class
xaml
user-interface
uwp
asked on Stack Overflow Oct 19, 2018 by Francois Gagnon • edited Oct 23, 2018 by halfer

2 Answers

0

This little bit related to MVVM pattern. But not complicated.

  1. Install MvvmLightLibs Nuget package.
  2. Register message listener in MainPage.xaml.cs constructor method.

    GalaSoft.MvvmLight.Messaging.Messenger.Default.Register(this, "count_changed", (value) =>
            {
                TopCmdBarContent.Text = "What ever you want";
            });
  1. In your InitializePlayList() method, send message

    public async void InitializePlayList() { // Your processing code .... .... ... GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(count.ToString(), "count_changed"); }

  2. That's all

Actually you can do these things using EventHandler, but for the easy convenience, here I recommend you to use MvvvmLightLibs

answered on Stack Overflow Oct 20, 2018 by Ashiq Hassan
0

Ok.... got it finally..... after much sweating and saying a whole lot of bad words.

Both approaches work (passing a reference to the TextBlock, or using the WMMW message, but it's the call to the dispatcher that was wrong. Here is the correct syntax, in case it helps someone:

      Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => {MessagePad = "blablabla";});

The syntax of this thing!

I've been doing programming for over 40 years now in an innumerable number of programming languages and I cannot believe someone thought this would be the proper way of saying this in c#, a new, modern programming language! .... god.... Being from Canada, I can now legally say that this chap must have been smoking some very, very good weed..... Makes me miss COBOL!

answered on Stack Overflow Oct 21, 2018 by Francois Gagnon

User contributions licensed under CC BY-SA 3.0