{"id":58002,"date":"2023-01-29T09:04:30","date_gmt":"2023-01-29T08:04:30","guid":{"rendered":"https:\/\/ar3dp.de\/docs\/programmieren\/net-maui-basis-applikation-app-template\/dipatching-uithread\/"},"modified":"2023-01-29T09:04:30","modified_gmt":"2023-01-29T08:04:30","slug":"dipatching-uithread","status":"publish","type":"docs","link":"https:\/\/ar3dp.de\/en\/docs\/programmieren\/net-maui-basis-applikation-app-template\/dipatching-uithread\/","title":{"rendered":"Dipatching &#038; UIThread"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Das App-Template nutzt die sogenannte &#8220;<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/architecture\/maui\/dependency-injection\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Dependency Injection<\/a>&#8220;, um zum Beispiel den aktuellen Dispatcher in das <strong>ViewModel<\/strong> zu injizieren.  Somit steht immer der richtige Dispatcher zur Verf\u00fcgung und Code, welcher auf dem <strong>UIThread<\/strong> ausgef\u00fchrt werden muss, kann einfach \u00fcber unseren <strong>DispatchManager<\/strong> ausgef\u00fchrt werden.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Constructor (ctor)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Damit eine View bzw. ein ViewModel auch den Dispatcher empfangen kann, muss der Ctor wie folgt angepasst werden (sofern Ihr eine neue View bzw. ein neues ViewModel anlegt).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">ViewModel<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Das neue <strong>ViewModel<\/strong> sollte immer vom <strong>AppViewModel<\/strong> erben. Damit erh\u00e4lt es Zugriff auf alle gemeinsam genutzten Funktionen und Methoden. Der Ctor muss als Parameter einen <code>IDispatcher<\/code> annehmen, und diesen auch an die Basis-Klasse weitergeben.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">public partial class DashboardPageViewModel : AppViewModel\n{\n    #region Ctor\n    public DashboardPageViewModel(IDispatcher dispatcher) : base(dispatcher)\n    {\n        Dispatcher = dispatcher;\n    }\n    #endregion\n\n    \/\/ Some code...\n}<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Views<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In der View muss der Ctor dann das gew\u00fcnschte ViewModel als Parameter \u00fcbergeben werden.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">public partial class DashboardPage : ContentPage\n{\n    public DashboardPage(DashboardPageViewModel viewModel)\n    {\n        InitializeComponent();\n        BindingContext = viewModel;\n    }\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">DispatchManager<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Der <strong>DispatchManager<\/strong> ist eine Helper-Klasse, welche es Ihnen einfach erm\u00f6glicht Code \u00fcber den <strong>Dispatcher<\/strong> auszuf\u00fchren. Durch das vorgeschaltete <strong>trycatch<\/strong> werden auch direkt Fehler abgefangen und protokoliert (<strong>EventManger<\/strong>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Einfaches Beispiel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Hier wird die Eigenschaft &#8220;IsRefreshing&#8221; Thread-Safe auf &#8220;false&#8221; gesetzt.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">DispatchManager.Dispatch(Dispatcher, () =&gt; IsRefreshing = false);<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Da es sich bei dieser Eigenschaft um ein Element handelt, welches ein Binding auf ein UI-Element hat, muss dieses zwangsweise am <span class=\"font-weight-semi-bold\">MainThread<\/span> der Applikation ausgef\u00fchrt werden. Andernfalls kann es hier zu einer Ausnahme bzw. einen Crash kommen.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">CodeSnippet dispatchen<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Um einen ganzen Codeblock zu dispatchen, kann die Funktion wie folgt aussehen.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">DispatchManager.Dispatch(Dispatcher, () =&gt;\n{\n    CurrentJobInfo = DemoManager.JobInfo;\n    PrintersState = DemoManager.PrinterState.Printer;\n    ServerSettings = DemoManager.ServerSettings;\n    Files = DemoManager.Files;\n});<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Dispatching abwarten<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Manchmal kann es n\u00f6tig sein, dass <strong>Dispatching<\/strong> abzuwarten (await). Dazu muss die <strong>DispatchAsync()<\/strong> Methode verwendet werden.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">await DispatchManager.DispatchAsync(Dispatcher, UpdateFromInstanceData);\n\/\/ Or\nawait DispatchManager.DispatchAsync(Dispatcher, () =&gt;\n{\n    IsRefreshing = true;\n    IsReachable = false;\n\n    SelectedServer = Servers.FirstOrDefault(server =&gt; server.Id == LastServerId);\n    SelectedServerAddress = SelectedServer?.FullWebAddress;\n});\nawait CheckOnlineStateSelectedServer();\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Das App-Template nutzt die sogenannte &#8220;Dependency Injection&#8220;, um zum Beispiel den aktuellen Dispatcher in das ViewModel zu injizieren. Somit steht immer der richtige Dispatcher zur Verf\u00fcgung und Code, welcher auf dem UIThread ausgef\u00fchrt werden muss, kann einfach \u00fcber unseren DispatchManager ausgef\u00fchrt werden. Constructor (ctor) Damit eine View bzw. ein ViewModel auch den Dispatcher empfangen kann,&#8230;<\/p>","protected":false},"author":1,"featured_media":0,"parent":57992,"menu_order":8,"comment_status":"open","ping_status":"closed","template":"","meta":{"_featured":false,"_is_vendor_doc":"0","footnotes":""},"doc_tag":[],"class_list":["post-58002","docs","type-docs","status-publish","hentry"],"comment_count":0,"_links":{"self":[{"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/58002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/comments?post=58002"}],"version-history":[{"count":1,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/58002\/revisions"}],"predecessor-version":[{"id":58683,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/58002\/revisions\/58683"}],"up":[{"embeddable":true,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/57992"}],"next":[{"title":"Firebase","link":"https:\/\/ar3dp.de\/en\/docs\/programmieren\/net-maui-basis-applikation-app-template\/firebase\/","href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/58257"}],"prev":[{"title":"Utilities & Helfer","link":"https:\/\/ar3dp.de\/en\/docs\/programmieren\/net-maui-basis-applikation-app-template\/utilities-helfer\/","href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/58001"}],"wp:attachment":[{"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/media?parent=58002"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/doc_tag?post=58002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}