我在使用 Xamarin.Forms 和 MVVM 制作游戲時遇到問題。
游戲中有一艘由用戶控制的潛艇,并且有地雷掉落,因此用戶必須避開這些地雷。地雷是在運行時使用 2 個計時器生成的,因此我用 XAML 中的 CollectionView 表示這些。
我已經使用 WPF(也使用 WinForms)制作了這個游戲,在這種情況下,我將 Canvas 用于游戲區域(用于地雷的 ItemsControl)并且系結運行良好。現在(使用 Xamarin.Forms)我嘗試使用 AbsoluteLayout 和 RelativeLayout 來實作游戲區域,但總是收到例如。以下輸出(RelativeLayout):
[0:] 系結:“160”無法轉換為“Xamarin.Forms.Constraint”型別
當前的 XAML 代碼:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SubmarineGame.View.GamePage"
Title="Submarine Game">
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<RelativeLayout Grid.Row="0">
<Image Source="sea.png"
RelativeLayout.WidthConstraint="{ConstraintExpression
Type=RelativeToParent,
Property=Width,
Factor=1}"
RelativeLayout.HeightConstraint="{ConstraintExpression
Type=RelativeToParent,
Property=Height,
Factor=1}"
Aspect="AspectFill" />
<ImageButton Command="{Binding ExitCommand}"
CornerRadius="50"
RelativeLayout.WidthConstraint="{ConstraintExpression Constant=50}"
RelativeLayout.HeightConstraint="{ConstraintExpression Constant=50}"
RelativeLayout.XConstraint="{ConstraintExpression
Type=RelativeToParent,
Property=Width,
Factor=0.9}"
RelativeLayout.YConstraint="{ConstraintExpression
Type=RelativeToParent,
Property=Height,
Factor=0.1}"
Source="pausebutton.png" />
<CollectionView ItemsSource="{Binding Mines}"
BackgroundColor="Transparent"
RelativeLayout.WidthConstraint="{ConstraintExpression
Type=RelativeToParent,
Property=Width,
Factor=1}"
RelativeLayout.HeightConstraint="{ConstraintExpression
Type=RelativeToParent,
Property=Height,
Factor=1}"
FlowDirection="MatchParent">
<CollectionView.ItemTemplate>
<DataTemplate>
<Image RelativeLayout.WidthConstraint="{ConstraintExpression Constant=64}"
RelativeLayout.HeightConstraint="{ConstraintExpression Constant=64}"
RelativeLayout.XConstraint="{Binding X}"
RelativeLayout.YConstraint="{Binding Y}"
Source="nuclearbomb.png" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Image RelativeLayout.WidthConstraint="{ConstraintExpression Constant=64}"
RelativeLayout.HeightConstraint="{ConstraintExpression Constant=64}"
RelativeLayout.XConstraint="{Binding Submarine.X}"
RelativeLayout.YConstraint="{Binding Submarine.Y}"
Source="submarine.png" />
</RelativeLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="Start" Grid.Row="1">
<Label Text="Game time: " />
<Label Text="{Binding GameTime}" />
</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="End" Grid.Row="1">
<Label Text="Destroyed mines: " />
<Label Text="{Binding DestroyedMineCount}" />
</StackLayout>
</Grid>
</ContentPage.Content>
</ContentPage>
ViewModel 中的 Submarine 和 Mines:
public ObservableCollection<Shape> Mines { get; set; }
public Submarine Submarine { get; set; }
形狀類:
public class Shape : ViewModelBase
{
private Int32 _x;
private Int32 _y;
public Int32 X
{
get { return _x; }
set
{
if (_x != value)
{
_x = value;
OnPropertyChanged();
}
}
}
public Int32 Y
{
get { return _y; }
set
{
if (_y != value)
{
_y = value;
OnPropertyChanged();
}
}
}
public Int32 Width { get; set; }
public Int32 Height { get; set; }
public Int32 Weight { get; set; }
}
潛艇類:
public class Submarine : Shape
{
public DelegateCommand StepCommand { get; set; }
}
所以問題是如何解決轉換問題?我是否為游戲區域(甚至地雷)使用了錯誤的布局?在這種情況下,Xamarin 開發人員使用什么方法或模式來實作 MVVM 架構?
感謝您的時間,并為我的英語感到抱歉!
uj5u.com熱心網友回復:
從錯誤中
[0:] Binding: '160' cannot be converted to type 'Xamarin.Forms.Constraint'
可以看出,它無法將整數轉換Constraint為系結運算式所期望的型別。
當你這樣做時:
<Image RelativeLayout.WidthConstraint="{ConstraintExpression Constant=64}"
RelativeLayout.HeightConstraint="{ConstraintExpression Constant=64}"
RelativeLayout.XConstraint="{Binding X}"
RelativeLayout.YConstraint="{Binding Y}"
Source="nuclearbomb.png" />
要系結到你的整數X和Y,當期望的型別是Constraint。
個人而言,我會避免使用約束的布局和偏好Grid,而不是,但如果你希望通過改變約束系結(型別,你可以解決這個錯誤X,Y等),以Constraint和使用輔助方法的數值轉換
Constraint.Constant(myConstraintAsADouble)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/399697.html
標籤:C# xaml xamarin.forms 虚拟机
