我在 Xamarin.Forms 中有一個計算器應用程式,我想實作 MVVM。現在,在我擁有 MVVM 檔案夾之前,我在 MainPage.xaml.cs 中創建了方法。當我創建 ViewModel 檔案夾時,我將 MainPage.xaml.cs 中的每個代碼都放到了 Methods.cs 中,而沒有更改方法本身。當然,“resultText”在當前背景關系中并不存在,所以我在網上深入搜索,發現我需要ICommand介面。我搜索了幾個小時來了解如何在我的代碼中實作介面,但它對我來說太復雜了,我一個字都聽不懂,浪費了幾個小時看數百個教程,但我無法理解如何實作它的資訊在我的代碼中。我所做的唯一一件事就是在類名之后放置:ICommand,因此它在我的代碼中生成了 2 個方法和 1 個事件,僅此而已,我不知道如何繼續。任何的想法?
方法.cs:
namespace Calculator.ViewModel
{
class Methods : ICommand
{
private decimal firstNumber;
private string operatorName;
private bool isOperatorClicked;
public event EventHandler CanExecuteChanged;
//public event PropertyChangedEventHandler PropertyChanged;
public void OnNumberButton_Clicked(object sender, EventArgs e)
{
var button = sender as Button;
if (resultText.Text == "0" || isOperatorClicked)
{
isOperatorClicked = false;
resultText.Text = button.Text;
}
else
{
resultText.Text = button.Text;
}
}
public void OnClearButton_Clicked(object sender, EventArgs e)
{
resultText.Text = "0";
}
public void OnOperationButton_Clicked(object sender, EventArgs e)
{
var button = sender as Button;
isOperatorClicked = true;
operatorName = button.Text;
firstNumber = Convert.ToDecimal(resultText.Text);
}
public void OnEqualButton_Clicked(object sender, EventArgs e)
{
try
{
decimal secondNumber = Convert.ToDecimal(resultText.Text);
string finalResult = Calculate(firstNumber, secondNumber).ToString("0.##");
resultText.Text = finalResult;
}
catch (Exception)
{
throw;
}
}
public decimal Calculate(decimal firstNumber, decimal secondNumber)
{
decimal result = 0;
if (operatorName == " ")
{
result = firstNumber secondNumber;
}
else if (operatorName == "-")
{
result = firstNumber - secondNumber;
}
else if (operatorName == "*")
{
result = firstNumber * secondNumber;
}
else if (operatorName == "/")
{
result = firstNumber / secondNumber;
}
return result;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
OnClearButton_Clicked;
}
}
}
主頁.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Calculator.MainPage">
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="Android" Value="20, 20" > </On>
</OnPlatform>
</ContentPage.Padding>
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="infostyle" TargetType="Button">
<Setter Property="WidthRequest" Value="60"/>
<Setter Property="HeightRequest" Value="60"/>
<Setter Property="TextColor" Value="White"/>
<Setter Property="FontSize" Value="25"/>
<Setter Property="BorderColor" Value="Black"/>
<Setter Property="BorderWidth" Value="1"/>
<Setter Property="BackgroundColor" Value="Green"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="topstyle" TargetType="Button">
<Setter Property="WidthRequest" Value="60"/>
<Setter Property="HeightRequest" Value="60"/>
<Setter Property="TextColor" Value="White"/>
<Setter Property="FontSize" Value="25"/>
<Setter Property="BorderColor" Value="Black"/>
<Setter Property="BorderWidth" Value="1"/>
<Setter Property="BackgroundColor" Value="Green"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="rightstyle" TargetType="Button">
<Setter Property="WidthRequest" Value="60"/>
<Setter Property="HeightRequest" Value="60"/>
<Setter Property="TextColor" Value="White"/>
<Setter Property="FontSize" Value="25"/>
<Setter Property="BorderColor" Value="Black"/>
<Setter Property="BorderWidth" Value="1"/>
<Setter Property="BackgroundColor" Value="Red"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="resultstyle" TargetType="Button">
<Setter Property="FontAttributes" Value="Bold"/>
<Setter Property="TextColor" Value="Black"/>
<Setter Property="FontSize" Value="38"/>
<Setter Property="HorizontalOptions" Value="End"/>
<Setter Property="VerticalOptions" Value="Center"/>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<Grid Padding="0, 0" RowSpacing="10" ColumnSpacing="10">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ContentView Padding="0, 0, 20, 0" Margin="0, 0, 0, 10" Grid.ColumnSpan="4" BackgroundColor="Brown">
<Label x:Name="resultText" Text="0" Style="{StaticResource resultstyle}">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="Android">
</On>
</OnPlatform>
</Label.FontFamily>
</Label>
</ContentView>
<Button Text="C" x:Name="btnClear" Grid.Column="0" Grid.Row="1" Style="{StaticResource topstyle}" Clicked="{Binding OnClearButton_Clicked}"/>
<Button x:Name="btnSave" Grid.Column="1" Grid.Row="1" Text="?" Style="{StaticResource topstyle}"/>
<Button Text="/" Grid.Column="2" Grid.Row="1" Style="{StaticResource topstyle}" Clicked= "{Binding OnOperationButton_Clicked}"/>
<Button Text="÷" Grid.Column="3" Grid.Row="1" Style="{StaticResource rightstyle}"/>
<Button Text="7" Grid.Column="0" Grid.Row="2" Style="{StaticResource infostyle}" Clicked= "{Binding OnNumberButton_Clicked}"/>
<Button Text="8" Grid.Column="1" Grid.Row="2" Style="{StaticResource infostyle}" Clicked= "{Binding OnNumberButton_Clicked}"/>
<Button Text="9" Grid.Column="2" Grid.Row="2" Style="{StaticResource infostyle}" Clicked= "{Binding OnNumberButton_Clicked}"/>
<Button Text="*" Grid.Column="3" Grid.Row="2" Style="{StaticResource rightstyle}" Clicked="{Binding OnOperationButton_Clicked}"/>
<Button Text="4" Grid.Column="0" Grid.Row="3" Style="{StaticResource infostyle}" Clicked= "{Binding OnNumberButton_Clicked}"/>
<Button Text="5" Grid.Column="1" Grid.Row="3" Style="{StaticResource infostyle}" Clicked= "{Binding OnNumberButton_Clicked}"/>
<Button Text="6" Grid.Column="2" Grid.Row="3" Style="{StaticResource infostyle}" Clicked= "{Binding OnNumberButton_Clicked}"/>
<Button Text="-" Grid.Column="3" Grid.Row="3" Style="{StaticResource rightstyle}" Clicked="{Binding OnOperationButton_Clicked}"/>
<Button Text="1" Grid.Column="0" Grid.Row="4" Style="{StaticResource infostyle}" Clicked= "{Binding OnNumberButton_Clicked}"/>
<Button Text="2" Grid.Column="1" Grid.Row="4" Style="{StaticResource infostyle}" Clicked="{Binding OnNumberButton_Clicked}"/>
<Button Text="3" Grid.Column="2" Grid.Row="4" Style="{StaticResource infostyle}" Clicked="{Binding OnNumberButton_Clicked}"/>
<Button Text=" " Grid.Column="3" Grid.Row="4" Grid.RowSpan="2" Style="{StaticResource rightstyle}" Clicked="{Binding OnOperationButton_Clicked}"/>
<Button Text="." Grid.Column="0" Grid.Row="5" Style="{StaticResource infostyle}" Clicked= "{Binding OnNumberButton_Clicked}"/>
<Button Text="0" Grid.Column="1" Grid.Row="5" Style="{StaticResource infostyle}" Clicked= "{Binding OnNumberButton_Clicked}"/>
<Button x:Name="btnEqual" Text="=" Grid.Column="2" Grid.Row="5" Style="{StaticResource infostyle}" Clicked= "{Binding OnEqualButton_Clicked}"/>
</Grid>
MainPage.xaml.cs:
namespace Calculator
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new Methods();
}
我想以某種方式轉換方法(如 OnEqualButton_Clicked 或 OnClearButton_Clicked 成為可系結命令
uj5u.com熱心網友回復:
您需要創建一個視圖模型來系結 bindingcontext,在建構式中初始化命令(代碼來自 xamarin 檔案):
class CommandDemoViewModel : INotifyPropertyChanged
{
double number = 1;
public event PropertyChangedEventHandler PropertyChanged;
public CommandDemoViewModel()
{
MultiplyBy2Command = new Command(() => Number *= 2);
DivideBy2Command = new Command(() => Number /= 2);
}
public double Number
{
set
{
if (number != value)
{
number = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Number"));
}
}
get
{
return number;
}
}
public ICommand MultiplyBy2Command { private set; get; }
public ICommand DivideBy2Command { private set; get; }
}
這是它的檔案和示例:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/button
https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/userinterface-buttondemos/
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/382425.html
標籤:C# 沙马林 xamarin.forms 虚拟机 数据绑定
上一篇:如何修復Xamarin“找不到檔案xamarin.android.glide.disklrucache\4.12.0.4\buildTransitive\monoandroid10.0\..
