uid |
---|
Uno.Workshop.Counter.CSharp.MVUX |
Download the complete C# Markup + MVUX sample
[!INCLUDE Intro]
In this tutorial, you will learn how to:
To complete this tutorial, you don't need any prior knowledge of the Uno Platform or C#.
[!INCLUDE VS]
[!NOTE] If you don't have the Uno Platform Extension for Visual Studio installed, follow these instructions.
Launch Visual Studio and click on Create new project on the Start Window. Alternatively, if you're already in Visual Studio, click New, Project from the File menu.
Type Uno Platform
in the search box
Click Uno Platform App, then Next
Name the project Counter
and click Create
At this point you'll enter the Uno Platform Template Wizard, giving you options to customize the generated application. For this tutorial, we're only going to configure the markup language and the presentation framework.
Select Blank in Presets selection
Select the Presentation tab and choose MVUX
Select the Markup tab and choose C# Markup
Before completing the wizard, take a look through each of the sections and see what other options are available. You can always come back and create a new project with different options later. For more information on all the template options, see Using the Uno Platform Template.
The template will create a solution with a single cross-platform project, named Counter
, ready to run.
[!NOTE] If you don't have the Uno Platform dotnet new templates installed, follow dotnet new templates for Uno Platform.
From the command line, run the following command:
dotnet new unoapp -preset blank -presentation mvux -markup csharp -o Counter
This will create a new folder called Counter containing the new application.
If you want to discover all the options available in the unoapp template, run the following command:
dotnet new unoapp -h
Also, for more information on all the template options, see Using the Uno Platform Template.
[!INCLUDE Counter Solution]
[!INCLUDE Main Window]
[!INCLUDE Main Page - C# Markup]
[!INCLUDE Main Page - Layout]
[!INCLUDE Main Page - Image]
[!INCLUDE Main Page - Change Layout]
[!INCLUDE Main Page - Other Elements]
[!INCLUDE Main Model]
Now that we have the MainModel
class, we can update the MainPage
to use data binding to connect the UI to the application logic.
Let's add the DataContext
to our page. To do so, add .DataContext(new MainViewModel(), (page, vm) => page
before .Background(...)
. Remember to close the DataContext
expression with a )
at the end of the code. It should look similar to the code below:
this.DataContext(new MainViewModel(), (page, vm) => page
.Background(ThemeResource.Get<Brush>("ApplicationPageBackgroundThemeBrush"))
.Content(
...
)
);
Update the TextBlock
by removing its current text content and replacing it with a binding expression for the Countable.Count
property of the MainViewModel
. Modify the existing Text
property with () => vm.Countable.Count, txt => $"Counter: {txt}"
. The adjusted code is as follows:
new TextBlock()
.Margin(12)
.HorizontalAlignment(HorizontalAlignment.Center)
.TextAlignment(Microsoft.UI.Xaml.TextAlignment.Center)
.Text(() => vm.Countable.Count, txt => $"Counter: {txt}")
Update the TextBox
by binding the Text
property to the Countable.Step
property of the MainViewModel. The Mode
of the binding is set to TwoWay
so that the Countable.Step
property is updated when the user changes the value in the TextBox
.
new TextBox()
.Margin(12)
.HorizontalAlignment(HorizontalAlignment.Center)
.TextAlignment(Microsoft.UI.Xaml.TextAlignment.Center)
.PlaceholderText("Step Size")
.Text(x => x.Binding(() => vm.Countable.Step).TwoWay())
Update the Button
to add a Command
property that is bound to the IncrementCounter
task of the MainViewModel
.
new Button()
.Margin(12)
.HorizontalAlignment(HorizontalAlignment.Center)
.Command(() => vm.IncrementCounter)
.Content("Increment Counter by Step Size")
The final code for MainPage.cs should look like this:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.DataContext(new MainViewModel(), (page, vm) => page
.Background(ThemeResource.Get<Brush>("ApplicationPageBackgroundThemeBrush"))
.Content(
new StackPanel()
.VerticalAlignment(VerticalAlignment.Center)
.Children(
new Image()
.Margin(12)
.HorizontalAlignment(HorizontalAlignment.Center)
.Width(150)
.Height(150)
.Source("ms-appx:///Assets/logo.png"),
new TextBox()
.Margin(12)
.HorizontalAlignment(HorizontalAlignment.Center)
.TextAlignment(Microsoft.UI.Xaml.TextAlignment.Center)
.PlaceholderText("Step Size")
.Text(x => x.Binding(() => vm.Countable.Step).TwoWay()),
new TextBlock()
.Margin(12)
.HorizontalAlignment(HorizontalAlignment.Center)
.TextAlignment(Microsoft.UI.Xaml.TextAlignment.Center)
.Text(() => vm.Countable.Count, txt => $"Counter: {txt}"),
new Button()
.Margin(12)
.HorizontalAlignment(HorizontalAlignment.Center)
.Command(() => vm.IncrementCounter)
.Content("Increment Counter by Step Size")
)
)
);
}
}
[!INCLUDE Wrap up]
If you want to see the completed application, you can download the source code from GitHub.
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )