0

I want to use the Snackbar from any thread. I declared my Snackbar as i should her; https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/Snackbar The Wiki says that I can only access the Snackbar from a Dispatcher thread, but how do I implement this?

the user control xaml;

<materialDesign:DialogHost SnackbarMessageQueue="{Binding ElementName=MySnackbar, Path=MessageQueue}" Identifier="DialogSnackbar">
    <Grid>
        <!-- app content here -->
        <materialDesign:Snackbar x:Name="MySnackbar" MessageQueue="{materialDesign:MessageQueue}" />
    </Grid>
</materialDesign:DialogHost>

to show a dialog, but i also want to hand over a message; implementation from https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/Dialogs

public static async void ShowDialog()
{
    var result = await DialogHost.Show("test", "DialogSnackbar", ExtendedOpenedEventHandler, ExtendedClosingEventHandler);
}

private static void ExtendedOpenedEventHandler(object sender, DialogOpenedEventArgs eventargs)
{
}         

private static void ExtendedClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
{         
}

the current message is displayed really weird

current

tuke307
  • 411
  • 5
  • 13

3 Answers3

1

All seems to look right for me.

In your ShowDialog() method you only pass a string "test" as content for your DialogHost and you didn't define a DataTemplate in DialogHost.DialogContentTemplate nor a DataTemplateSelector in DialogHost.DialogContentTemplateSelector. So the default behavior of ContentControl kicks in when there is no ContentTemplate or ContentTemplateSelector defined and you don't pass the XAML elements directly as Content. This results in a TextBlock element being created for the dialog content where your string is bound to its Text property. This is exactly what your picture shows.

So to get a different result than what your picture shows you need to either pass directly your XAML elements which you want to show in your dialog (with a root container element and all buttons your dialog needs) or define a DataTemplate or DataTemplateSelector for your DialogHost in your XAML, if you want to use it in a MVVM scenario.

Look at this example from the repo if you need a hint how you can implement this.

Anateus
  • 439
  • 4
  • 13
0

You can use:

App.Current.Dispatcher.Invoke(()=>{

//Add the control related code stuff here

});
rufw91
  • 157
  • 2
  • 8
-1

with the help of @Anateus i get to this solution;

in my MainWindow.xaml i declared this;

<materialDesign:DialogHost  VerticalAlignment="Bottom"
                            HorizontalAlignment="Center"
                            Identifier="DialogSnackbar">
</materialDesign:DialogHost>

The DialogSnackbarView.xaml - the content of the dialog;

<UserControl ...>
    <Grid>
        <materialDesign:Snackbar x:Name="MySnackbar"
                                 MessageQueue="{materialDesign:MessageQueue}" />
    </Grid>
</UserControl>

to show the Dialog everywhere;

var view = new DialogSnackbarView();
view.MySnackbar.MessageQueue.Enqueue("test");
await DialogHost.Show(view, "DialogSnackbar");
tuke307
  • 411
  • 5
  • 13
  • Anateus helped to find a solution, but you didn't award the bounty. Not very nice. You can't award you own answer and get reputation points back. – ASh Aug 13 '20 at 09:05
  • Sorry I didn't know that. Next time I will reward the bounty. I didnt rewarded it because nobody gave me a real solution to my problem, only thougts... – tuke307 Aug 13 '20 at 12:53