Sfoglia il codice sorgente

Добавьте файлы проекта.

МухинаЛВ 1 mese fa
parent
commit
7b0328b499

+ 25 - 0
AvaloniaMVVM.sln

@@ -0,0 +1,25 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.10.35027.167
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaMVVM", "AvaloniaMVVM\AvaloniaMVVM.csproj", "{C2F50E31-56EB-4A41-9AB2-2EA3AD149CD4}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{C2F50E31-56EB-4A41-9AB2-2EA3AD149CD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{C2F50E31-56EB-4A41-9AB2-2EA3AD149CD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{C2F50E31-56EB-4A41-9AB2-2EA3AD149CD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{C2F50E31-56EB-4A41-9AB2-2EA3AD149CD4}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+	GlobalSection(ExtensibilityGlobals) = postSolution
+		SolutionGuid = {C40E90EF-36C2-419B-86E7-818A19DA4638}
+	EndGlobalSection
+EndGlobal

+ 15 - 0
AvaloniaMVVM/App.axaml

@@ -0,0 +1,15 @@
+<Application xmlns="https://github.com/avaloniaui"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             x:Class="AvaloniaMVVM.App"
+             xmlns:local="using:AvaloniaMVVM"
+             RequestedThemeVariant="Default">
+             <!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
+
+    <Application.DataTemplates>
+        <local:ViewLocator/>
+    </Application.DataTemplates>
+  
+    <Application.Styles>
+        <FluentTheme />
+    </Application.Styles>
+</Application>

+ 29 - 0
AvaloniaMVVM/App.axaml.cs

@@ -0,0 +1,29 @@
+using Avalonia;
+using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Markup.Xaml;
+using AvaloniaMVVM.ViewModels;
+using AvaloniaMVVM.Views;
+
+namespace AvaloniaMVVM
+{
+    public partial class App : Application
+    {
+        public override void Initialize()
+        {
+            AvaloniaXamlLoader.Load(this);
+        }
+
+        public override void OnFrameworkInitializationCompleted()
+        {
+            if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
+            {
+                desktop.MainWindow = new MainWindow
+                {
+                    DataContext = new MainWindowViewModel(),
+                };
+            }
+
+            base.OnFrameworkInitializationCompleted();
+        }
+    }
+}

BIN
AvaloniaMVVM/Assets/avalonia-logo.ico


+ 24 - 0
AvaloniaMVVM/AvaloniaMVVM.csproj

@@ -0,0 +1,24 @@
+<Project Sdk="Microsoft.NET.Sdk">
+  <PropertyGroup>
+    <OutputType>WinExe</OutputType>
+    <TargetFramework>net8.0</TargetFramework>
+    <Nullable>enable</Nullable>
+    <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
+    <ApplicationManifest>app.manifest</ApplicationManifest>
+    <AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <AvaloniaResource Include="Assets\**" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Avalonia" Version="11.1.0" />
+    <PackageReference Include="Avalonia.Desktop" Version="11.1.0" />
+    <PackageReference Include="Avalonia.Themes.Fluent" Version="11.1.0" />
+    <PackageReference Include="Avalonia.Fonts.Inter" Version="11.1.0" />
+    <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
+    <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.0" />
+    <PackageReference Include="Avalonia.ReactiveUI" Version="11.1.0" />
+  </ItemGroup>
+</Project>

+ 14 - 0
AvaloniaMVVM/Models/ModelClass.cs

@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AvaloniaMVVM.Models
+{
+    internal class ModelClass
+    {
+        public static int count = 0;
+        public static List<string> surnameList = new List<string> { "Иванов", "Петров", "Сидоров" };
+    }
+}

+ 24 - 0
AvaloniaMVVM/Program.cs

@@ -0,0 +1,24 @@
+using Avalonia;
+using Avalonia.ReactiveUI;
+using System;
+
+namespace AvaloniaMVVM
+{
+    internal sealed class Program
+    {
+        // Initialization code. Don't use any Avalonia, third-party APIs or any
+        // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
+        // yet and stuff might break.
+        [STAThread]
+        public static void Main(string[] args) => BuildAvaloniaApp()
+            .StartWithClassicDesktopLifetime(args);
+
+        // Avalonia configuration, don't remove; also used by visual designer.
+        public static AppBuilder BuildAvaloniaApp()
+            => AppBuilder.Configure<App>()
+                .UsePlatformDetect()
+                .WithInterFont()
+                .LogToTrace()
+                .UseReactiveUI();
+    }
+}

+ 34 - 0
AvaloniaMVVM/ViewLocator.cs

@@ -0,0 +1,34 @@
+using Avalonia.Controls;
+using Avalonia.Controls.Templates;
+using AvaloniaMVVM.ViewModels;
+using System;
+
+namespace AvaloniaMVVM
+{
+    public class ViewLocator : IDataTemplate
+    {
+
+        public Control? Build(object? data)
+        {
+            if (data is null)
+                return null;
+
+            var name = data.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal);
+            var type = Type.GetType(name);
+
+            if (type != null)
+            {
+                var control = (Control)Activator.CreateInstance(type)!;
+                control.DataContext = data;
+                return control;
+            }
+
+            return new TextBlock { Text = "Not Found: " + name };
+        }
+
+        public bool Match(object? data)
+        {
+            return data is ViewModelBase;
+        }
+    }
+}

+ 103 - 0
AvaloniaMVVM/ViewModels/MainWindowViewModel.cs

@@ -0,0 +1,103 @@
+using Avalonia.Controls;
+using Avalonia.Controls.Shapes;
+using Avalonia.Media;
+using Avalonia.Threading;
+using AvaloniaMVVM.Models;
+using ReactiveUI;
+using System;
+using System.Collections.Generic;
+
+namespace AvaloniaMVVM.ViewModels
+{
+    
+    public class MainWindowViewModel : ViewModelBase
+    {
+        DispatcherTimer timer = new DispatcherTimer();
+        public MainWindowViewModel()
+        {
+            
+            timer.Interval = new TimeSpan(0, 0, 5);
+            timer.Tick += new EventHandler(timer_tick);
+        }
+        #region
+        public int Count
+        {
+            get => ModelClass.count;
+            set => this.RaiseAndSetIfChanged(ref ModelClass.count, value);
+        }
+
+        public List<string> SurnameList => ModelClass.surnameList;
+
+        public string SelectedSurname => ModelClass.surnameList[0];
+
+        public void AddCount()
+        {
+            Count++;
+        }
+        #endregion
+
+        Canvas _can;
+
+        public Canvas Can
+        {
+            get => _can;
+            set => this.RaiseAndSetIfChanged(ref _can, value);
+        }
+
+
+        private void timer_tick(object sender, EventArgs e)
+        {
+            CreateCaptha();
+        }
+
+        public void CreateCaptha()
+        {
+           
+
+            Random rnd = new Random();
+            SolidColorBrush color = new SolidColorBrush(Color.FromRgb(Convert.ToByte(rnd.Next(256)), Convert.ToByte(rnd.Next(255)), Convert.ToByte(rnd.Next(255))));
+            
+
+            Canvas canvas = new Canvas()
+            {
+                Width = 400,
+                Height = 400,
+                Background = color
+            };
+
+            for (int i=0; i<3; i++)
+            {
+            TextBlock number = new TextBlock()
+            {
+                Text = rnd.Next(10).ToString(),
+                FontSize = 150,
+                Foreground = Brushes.Black,
+                Padding = new Avalonia.Thickness(rnd.Next(100), rnd.Next(50))
+            };
+
+            canvas.Children.Add(number);
+            }
+            
+
+            for (int i=0; i<20; i++) {
+                Line line = new Line()
+                {
+                  StartPoint = new Avalonia.Point(rnd.Next(400), rnd.Next(400)),
+                  EndPoint = new Avalonia.Point(rnd.Next(400), rnd.Next(400)),
+                  Stroke = Brushes.White,
+                  StrokeThickness = 3
+                };
+
+            canvas.Children.Add(line);
+            }
+            timer.Start();
+            Can = canvas;
+        }
+    }
+
+
+
+
+
+
+}

+ 8 - 0
AvaloniaMVVM/ViewModels/ViewModelBase.cs

@@ -0,0 +1,8 @@
+using ReactiveUI;
+
+namespace AvaloniaMVVM.ViewModels
+{
+    public class ViewModelBase : ReactiveObject
+    {
+    }
+}

+ 26 - 0
AvaloniaMVVM/Views/MainWindow.axaml

@@ -0,0 +1,26 @@
+<Window xmlns="https://github.com/avaloniaui"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:vm="using:AvaloniaMVVM.ViewModels"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
+        x:Class="AvaloniaMVVM.Views.MainWindow"
+        x:DataType="vm:MainWindowViewModel"
+        Icon="/Assets/avalonia-logo.ico"
+        Title="AvaloniaMVVM" FontSize="26">
+
+    <Design.DataContext>
+        <!-- This only sets the DataContext for the previewer in an IDE,
+             to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
+        <vm:MainWindowViewModel/>
+    </Design.DataContext>
+
+	<StackPanel>
+		<TextBlock Text="{Binding Count}"></TextBlock>
+		<Button Command="{Binding AddCount}">Увеличить на 1</Button>
+		<TextBlock Text="{Binding #cm.SelectedValue}"></TextBlock>
+		<ComboBox Name="cm" ItemsSource="{Binding SurnameList}" SelectedItem="{Binding SelectedSurname}"></ComboBox>
+		<Button Command="{Binding CreateCaptha}">Сгенерировать</Button>
+		<UserControl Content="{Binding Can}"></UserControl>
+	</StackPanel>
+</Window>

+ 12 - 0
AvaloniaMVVM/Views/MainWindow.axaml.cs

@@ -0,0 +1,12 @@
+using Avalonia.Controls;
+
+namespace AvaloniaMVVM.Views
+{
+    public partial class MainWindow : Window
+    {
+        public MainWindow()
+        {
+            InitializeComponent();
+        }
+    }
+}

+ 18 - 0
AvaloniaMVVM/app.manifest

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
+  <!-- This manifest is used on Windows only.
+       Don't remove it as it might cause problems with window transparency and embedded controls.
+       For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
+  <assemblyIdentity version="1.0.0.0" name="AvaloniaMVVM.Desktop"/>
+
+  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
+    <application>
+      <!-- A list of the Windows versions that this application has been tested on
+           and is designed to work with. Uncomment the appropriate elements
+           and Windows will automatically select the most compatible environment. -->
+
+      <!-- Windows 10 -->
+      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
+    </application>
+  </compatibility>
+</assembly>