WPF 音量拨号代码教程代码在我的代码中有问题但在教程代码中 none

WPF volume dial code tutorial code has issues in my code but none in the tutorial code

嘿,我需要一些帮助才能使下面的代码正常工作。我一直在关注 this tutorial,大多数事情都有效,但代码中的一些事情似乎由于某种原因不起作用。所有这些代码看起来都像教程中的代码,所以我不确定发生了什么?

用户控制xaml代码:

<UserControl x:Class="carProg.dial"
             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:carProgForKids"
             mc:Ignorable="d" Height="450" Width="450">
    <Grid Manipulation.ManipulationMode="All" ManipulationDelta="Grid_ManipulationDelta">
        <Ellipse Stroke="Black" Margin="30" Fill="#FFFFE15D"/>
        <Grid>
            <Grid.RenderTransform>
                <RotateTransform CenterX="225" CenterY="225" Angle="{Binding Angle}" />
            </Grid.RenderTransform>
            <Ellipse Fill="#FFFFFDF3" 
                     Height="100" Stroke="Black" Margin="0"
                     VerticalAlignment="Top" Width="100"/>
        </Grid>
    </Grid>
</UserControl>

后面的代码:

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace carProg
{
    public partial class dial : UserControl, INotifyPropertyChanged
    {
        public dial()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private void Grid_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            this.Angle = GetAngle(e.Position, this.RenderSize);
            this.Amount = (int)(this.Angle / 360 * 100);
        }

        int m_Amount = default(int);
        public int Amount { 
            get { return m_Amount; } 
            set { SetProperty(ref m_Amount, value); 
        } }

        double m_Angle = default(double);
        public double Angle { 
            get { return m_Angle; } 
            set { SetProperty(ref m_Angle, value); 
        } }

        public event PropertyChangedEventHandler PropertyChanged;
        void SetProperty<T>(ref T storage, T value, [System.Runtime.CompilerServices.CallerMemberName]);

        public enum Quadrants : int { nw = 2, ne = 1, sw = 4, se = 3 }
        private double GetAngle(Point touchPoint, Size circleSize)
        {
            var _X = touchPoint.X - (circleSize.Width / 2d);
            var _Y = circleSize.Height - touchPoint.Y - (circleSize.Height / 2d);
            var _Hypot = Math.Sqrt(_X * _X + _Y * _Y);
            var _Value = Math.Asin(_Y / _Hypot) * 180 / Math.PI;
            var _Quadrant = (_X >= 0) ?
                (_Y >= 0) ? Quadrants.ne : Quadrants.se :
                (_Y >= 0) ? Quadrants.nw : Quadrants.sw;
            switch (_Quadrant)
            {
                case Quadrants.ne: _Value = 090 - _Value; break;
                case Quadrants.nw: _Value = 270 + _Value; break;
                case Quadrants.se: _Value = 090 - _Value; break;
                case Quadrants.sw: _Value = 270 + _Value; break;
            }
            return _Value;
        }
    }
}

目前的问题是:

e.Position 错误:

Error CS1061 'ManipulationDeltaEventArgs' does not contain a definition for 'Position' and no accessible extension method 'Position' accepting a first argument of type 'ManipulationDeltaEventArgs' could be found (are you missing a using directive or an assembly reference?)

设置属性 错误:

Error CS7036 There is no argument given that corresponds to the required formal parameter '' of 'dial.SetProperty(ref T, T, ?)'

CallerMemberName 错误:

Error CS1061 'ManipulationDeltaEventArgs' does not contain a definition for 'Position' and no accessible extension method 'Position' accepting a first argument of type 'ManipulationDeltaEventArgs' could be found (are you missing a using directive or an assembly reference?)

并且在 xaml 用户控件上:

<网格Manipulation.ManipulationMode="全部" 错误:

Error Manipulation is not active on the specified element.

不确定为什么我会收到这些错误,因为它与视频教程的代码相同,而且没有像我的那样显示错误..

更新 #1

更新#2 该代码应产生一个拨号盘:

更新 #3:让鼠标充当触摸设备

BlakeNUI 中添加 MouseTouchDevice.cs class:

public class MouseTouchDevice : TouchDevice, ITouchDevice
{
//Class Members

private static MouseTouchDevice device;

public Point Position { get; set; }

//Public Static Methods

public static void RegisterEvents(FrameworkElement root)
{
    root.PreviewMouseDown += MouseDown;
    root.PreviewMouseMove += MouseMove;
    root.PreviewMouseUp += MouseUp;
    root.LostMouseCapture += LostMouseCapture;
    root.MouseLeave += MouseLeave;
}

//Private Static Methods

private static void MouseDown(object sender, MouseButtonEventArgs e)
{
    if (device != null &&
        device.IsActive)
    {
        device.ReportUp();
        device.Deactivate();
        device = null;
    }
    device = new MouseTouchDevice(e.MouseDevice.GetHashCode());
    device.SetActiveSource(e.MouseDevice.ActiveSource);
    device.Position = e.GetPosition(null);
    device.Activate();
    device.ReportDown();
}

private static void MouseMove(object sender, MouseEventArgs e)
{
    if (device != null &&
        device.IsActive)
    {
        device.Position = e.GetPosition(null);
        device.ReportMove();
    }
}

private static void MouseUp(object sender, MouseButtonEventArgs e)
{
    LostMouseCapture(sender, e);
}

static void LostMouseCapture(object sender, MouseEventArgs e)
{
    if (device != null &&
        device.IsActive)
    {
        device.Position = e.GetPosition(null);
        device.ReportUp();
        device.Deactivate();
        device = null;
    }
}

static void MouseLeave(object sender, MouseEventArgs e)
{
    LostMouseCapture(sender, e);
}

//Constructors

public MouseTouchDevice(int deviceId) :
    base(deviceId)
{
    Position = new Point();
}

//Overridden methods

public override TouchPointCollection GetIntermediateTouchPoints(IInputElement relativeTo)
{
    return new TouchPointCollection();
}

public override TouchPoint GetTouchPoint(IInputElement relativeTo)
{
    Point point = Position;
}

到我的项目并放置这个:

public watchingMovie(String movie)
{
    InitializeComponent();
    MouseTouchDevice.RegisterEvents(this);
    ...
}

让它像在触摸设备上一样工作。

尝试在事件处理程序中将e.Position替换为e.ManipulationOrigin并将Grid中的IsManipulationEnabled属性设置为true XAML 标记:

<Grid  IsManipulationEnabled="True" ...

您还需要实际实现 SetProperty<T> 方法。

目前的问题是:

Error CS1061 'ManipulationDeltaEventArgs' does not contain a definition for 'Position' and no accessible extension method 'Position' accepting a first argument of type 'ManipulationDeltaEventArgs' could be found (are you missing a using directive or an assembly reference?)

此错误是因为它不存在,您可以将 .Position 替换为 ManipulationOrigin 以获取操作起源点和 returns a System.Windows.Point :

 this.Angle = GetAngle(e.ManipulationOrigin, this.RenderSize);
 

Error CS7036 There is no argument given that corresponds to the required formal parameter '' of 'dial.SetProperty(ref T, T, ?)'

请参阅下面的示例实现:

 public event PropertyChangedEventHandler PropertyChanged;
 protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
 protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propName = "")
 {
    if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
    storage = value;
    OnPropertyChanged(propName);
    return true;
 }

Error Manipulation is not active on the specified element.

要解决此问题,请在网格本身上设置 IsManipulationEnabled="True",这意味着为 UIElement

启用了操作

所有这些都结束了:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace carProg
{
    /// <summary>
    /// Interaction logic for dial.xaml
    /// </summary>
    public partial class dial : UserControl, INotifyPropertyChanged
    {
        public dial()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private void Grid_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            this.Angle = GetAngle(e.ManipulationOrigin, this.RenderSize);
            this.Amount = (int)(this.Angle / 360 * 100);
        }

        int m_Amount = default(int);
        public int Amount
        {
            get { return m_Amount; }
            set
            {
                SetProperty(ref m_Amount, value);
            }
        }

        double m_Angle = default(double);
        public double Angle
        {
            get { return m_Angle; }
            set
            {
                SetProperty(ref m_Angle, value);
            }
        }

        // Please note the changes with OnPropertyChanged and SetProperty
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propName = "")
        {
            if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
            storage = value;
            OnPropertyChanged(propName);
            return true;
        }

        public enum Quadrants : int { nw = 2, ne = 1, sw = 4, se = 3 }
        private double GetAngle(Point touchPoint, Size circleSize)
        {
            var _X = touchPoint.X - (circleSize.Width / 2d);
            var _Y = circleSize.Height - touchPoint.Y - (circleSize.Height / 2d);
            var _Hypot = Math.Sqrt(_X * _X + _Y * _Y);
            var _Value = Math.Asin(_Y / _Hypot) * 180 / Math.PI;
            var _Quadrant = (_X >= 0) ?
                (_Y >= 0) ? Quadrants.ne : Quadrants.se :
                (_Y >= 0) ? Quadrants.nw : Quadrants.sw;
            switch (_Quadrant)
            {
                case Quadrants.ne: _Value = 090 - _Value; break;
                case Quadrants.nw: _Value = 270 + _Value; break;
                case Quadrants.se: _Value = 090 - _Value; break;
                case Quadrants.sw: _Value = 270 + _Value; break;
            }
            return _Value;
        }
    }
}

// Please note: IsManipulationEnabled on the grid
<Grid IsManipulationEnabled="True" ManipulationDelta="Grid_ManipulationDelta">
        <Ellipse Margin="30"
                 Fill="#FFFFE15D"
                 Stroke="Black"
                 />
        <Grid>
            <Grid.RenderTransform>
                <RotateTransform Angle="{Binding Angle}" CenterX="225" CenterY="225" />
            </Grid.RenderTransform>
            <Ellipse Width="100"
                     Height="100"
                     Margin="0"
                     VerticalAlignment="Top"
                     Fill="#FFFFFDF3"
                     Stroke="Black"
                     />
        </Grid>
    </Grid>