I have a UWP app that has XAML pages and class libraries to help aggregate certain functions.
I have certain objects (dynamic) that have a property called Brush. This property holds any Brush that can be bind in XAML to indicate the brush used to draw a control/item within a control with this brush. This is all wrapped in various converters to used during Binding.
I am running into several issues:
Running the converter from a non-UI dispatcher throws the "Marshalled For Another Thread" error.
Wrapping the new SolidColorBrush(color) within a Dispatcher.RunAsync method, causes a deadlock.
I create a new function that is async to make it more convenient to run it within a sync method.
Here is the code for the converter (called RandomBrush):
return Task.Run(async () => await Data.CreateSolidColorBrush(color)).Result;
Here is the code for Data.CreateSolidColorBrush function:
public async static Task<Brush> CreateSolidColorBrush(Color color)
{
Brush brush = null;
var dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;
//var dispatcher = Window.Current.Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
try { brush = new SolidColorBrush(color); }
catch (Exception ex)
{ }
});
return brush;
}
I have tried various methods of async/task/action/etc but some don't run the action at all and some cause a deadlock.
What is the best way to create a brush from a color without all the complications!