Forráskód Böngészése

Don't use global variables in ControlCatalog

Nikita Tsukanov 6 éve
szülő
commit
9efdffe875

+ 13 - 0
samples/ControlCatalog/Pages/ContextMenuPage.xaml.cs

@@ -1,3 +1,4 @@
+using System;
 using Avalonia.Controls;
 using Avalonia.Markup.Xaml;
 using ControlCatalog.ViewModels;
@@ -12,6 +13,18 @@ namespace ControlCatalog.Pages
             DataContext = new ContextMenuPageViewModel();
         }
 
+        private ContextMenuPageViewModel _model;
+        protected override void OnDataContextChanged(EventArgs e)
+        {
+            if (_model != null)
+                _model.View = null;
+            _model  = DataContext as ContextMenuPageViewModel;
+            if (_model != null)
+                _model.View = this;
+
+            base.OnDataContextChanged(e);
+        }
+
         private void InitializeComponent()
         {
             AvaloniaXamlLoader.Load(this);

+ 14 - 0
samples/ControlCatalog/Pages/MenuPage.xaml.cs

@@ -1,3 +1,4 @@
+using System;
 using System.Collections.Generic;
 using System.Reactive;
 using System.Threading.Tasks;
@@ -21,5 +22,18 @@ namespace ControlCatalog.Pages
         {
             AvaloniaXamlLoader.Load(this);
         }
+        
+        private MenuPageViewModel _model;
+        protected override void OnDataContextChanged(EventArgs e)
+        {
+            if (_model != null)
+                _model.View = null;
+            _model  = DataContext as MenuPageViewModel;
+            if (_model != null)
+                _model.View = this;
+
+            base.OnDataContextChanged(e);
+        }
+        
     }
 }

+ 6 - 1
samples/ControlCatalog/ViewModels/ContextMenuPageViewModel.cs

@@ -2,12 +2,14 @@
 using System.Reactive;
 using System.Threading.Tasks;
 using Avalonia.Controls;
+using Avalonia.VisualTree;
 using ReactiveUI;
 
 namespace ControlCatalog.ViewModels
 {
     public class ContextMenuPageViewModel
     {
+        public Control View { get; set; }
         public ContextMenuPageViewModel()
         {
             OpenCommand = ReactiveCommand.CreateFromTask(Open);
@@ -48,8 +50,11 @@ namespace ControlCatalog.ViewModels
 
         public async Task Open()
         {
+            var window = View?.GetVisualRoot() as Window;
+            if (window == null)
+                return;
             var dialog = new OpenFileDialog();
-            var result = await dialog.ShowAsync(App.Current.MainWindow);
+            var result = await dialog.ShowAsync(window);
 
             if (result != null)
             {

+ 6 - 1
samples/ControlCatalog/ViewModels/MenuPageViewModel.cs

@@ -3,12 +3,14 @@ using System.Reactive;
 using System.Reactive.Linq;
 using System.Threading.Tasks;
 using Avalonia.Controls;
+using Avalonia.VisualTree;
 using ReactiveUI;
 
 namespace ControlCatalog.ViewModels
 {
     public class MenuPageViewModel
     {
+        public Control View { get; set; }
         public MenuPageViewModel()
         {
             OpenCommand = ReactiveCommand.CreateFromTask(Open);
@@ -65,8 +67,11 @@ namespace ControlCatalog.ViewModels
 
         public async Task Open()
         {
+            var window = View?.GetVisualRoot() as Window;
+            if (window == null)
+                return;
             var dialog = new OpenFileDialog();
-            var result = await dialog.ShowAsync(App.Current.MainWindow);
+            var result = await dialog.ShowAsync(window);
 
             if (result != null)
             {