老实说,我真的是在使用Microsoft的WPF框架进行首次亮相。我现在想做的只是一个简单的骰子动画,当单击按钮时其值会更改,并且也会影响滚动。我觉得绑定是正确的,并且一切都很好,但是当我启动该程序时,它可以很好地启动但无法正常工作。请我需要一些帮助。提供了所有代码
当我可视化我的代码时,我不知道,但是我认为是或者当我试图同时对dieContol.dataContext
和slider.dataContext
都影响同一对象时,或者可能是我滚动功能未正确完成。
这是我的MainWindow.xaml.cs
namespace Yahtzee
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
slider.DataContext = dieControl.DataContext = new ViewDieModel();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
((ViewDieModel)dieControl.DataContext).Rolling();
}
}
}
ControlDie.xaml.cs
namespace Yahtzee
{
/// <summary>
/// Interaction logic for test.xaml
/// </summary>
public partial class ControlDie : UserControl
{
public ControlDie()
{
InitializeComponent();
this.SizeChanged += (obj, args) => FaceDrawing();
}
public int DieValue
{
get { return (int)GetValue(DieValueProperty); }
set { SetValue(DieValueProperty, value); }
}
private void OnDieValueChange()
{
FaceDrawing();
}
private void FaceDrawing()
{
switch (DieValue)
{
case 1:
FaceOne();
break;
case 2:
FaceTwo();
break;
case 3:
FaceThree();
break;
case 4:
FaceFour();
break;
case 5:
FaceFive();
break;
case 6:
FaceSix();
break;
default:
FaceDrawError();
break;
}
}
private void FaceDrawError()
{
canvas.Children.Clear();
}
private void FaceOne()
{
canvas.Children.Clear();
DrawEllipse(.5, .5);
}
private void FaceTwo()
{
canvas.Children.Clear();
DrawEllipse(.25, .25);
DrawEllipse(.75, .75);
}
private void FaceThree()
{
canvas.Children.Clear();
DrawEllipse(.25, .75);
DrawEllipse(.5, .5);
DrawEllipse(.75, .25);
}
private void FaceFour()
{
canvas.Children.Clear();
DrawEllipse(.25, .25);
DrawEllipse(.25, .75);
DrawEllipse(.75, .25);
DrawEllipse(.75, .75);
}
private void FaceFive()
{
canvas.Children.Clear();
DrawEllipse(.25, .25);
DrawEllipse(.25, .75);
DrawEllipse(.5, .5);
DrawEllipse(.75, .25);
DrawEllipse(.75, .75);
}
private void FaceSix()
{
canvas.Children.Clear();
DrawEllipse(.25, .25);
DrawEllipse(.25, .5);
DrawEllipse(.25, .75);
DrawEllipse(.75, .25);
DrawEllipse(.75, .5);
DrawEllipse(.75, .75);
}
private void DrawEllipse(double v1, double v2)
{
var dieEllipse = new Ellipse();
dieEllipse.Fill = Brushes.Black;
dieEllipse.Width = 5;
dieEllipse.Height = 5;
Canvas.SetLeft(dieEllipse, v1 * Width - dieEllipse.Width / 2);
Canvas.SetTop(dieEllipse, v2 * Height - dieEllipse.Height / 2);
canvas.Children.Add(dieEllipse);
}
public static readonly DependencyProperty DieValueProperty = DependencyProperty.Register("DieValue", typeof(int), typeof(ControlDie), new PropertyMetadata(1, DieValueChanged));
public static void DieValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var controlDie = (ControlDie)obj;
controlDie.OnDieValueChange();
}
}
}
ControlDie.xaml
<UserControl x:Class="Yahtzee.ControlDie"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Yahtzee"
mc:Ignorable="d"
d:DesignHeight="48" d:DesignWidth="48">
<Border Width="{Binding ElementName=die, Path=Width}"
Height="{Binding ElementName=die, Path=Height}"
BorderBrush="Black"
BorderThickness="1"
CornerRadius="5">
<Canvas x:Name="canvas"/>
</Border>
</UserControl>
ViewDieModel.cs
namespace Yahtzee
{
class ViewDieModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public int FaceDie
{
get;set;
}
public void Rolling()
{
FaceDie = new Random().Next(6) + 1;
}
private void NotifyPropertyChange(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}
MainWindow.xaml
<Window x:Class="Yahtzee.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Yahtzee"
xmlns:myControl ="clr-namespace:Yahtzee"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel Orientation="Vertical">
<myControl:ControlDie x:Name="dieControl" Width="48" Height="48" DieValue="{Binding FaceDie}"/>
<Slider x:Name="slider" Minimum="1" Maximum="6" Value="{Binding FaceDie}"/>
<Button Content="Roll !" Click="Button_Click"/>
</StackPanel>
</Window>
RollingDice.cs
namespace Yahtzee
{
class RollingDice
{
private readonly int[] dice;
public RollingDice(params int[] dice)
{
if(dice.Length != 5)
{
throw new ArgumentException();
}
else
{
this.dice = dice.ToArray();
}
}
public int this[int index]
{
get
{
return dice[index];
}
}
public IList<int> generateList()
{
return new List<int>(dice);
}
public ISet<int> generateSet()
{
return new HashSet<int>(dice);
}
/*public FrequencyTable<int> AsFrequencyTable()
{
return new FrequencyTable<int>(dice);
}*/
}
}
我真诚地期望的实际结果是,单击按钮滚动骰子时,ControlDie会更改,而Slider也会更改,但实际上没有任何更改。