Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F#WPF:用户控件:与RegisterRoutedEvent一起安装的DependencyProperty_Wpf_F#_Event Handling_Wpf Controls_Dependency Properties - Fatal编程技术网

F#WPF:用户控件:与RegisterRoutedEvent一起安装的DependencyProperty

F#WPF:用户控件:与RegisterRoutedEvent一起安装的DependencyProperty,wpf,f#,event-handling,wpf-controls,dependency-properties,Wpf,F#,Event Handling,Wpf Controls,Dependency Properties,在F#中,我无法执行请求对象中描述的操作 缺少的一点是C事件的F#实现,声明如下: public event RoutedEventHandler DataChanged { add { base.AddHandler(ucData.DataChangedEvent, value); } remove { base.RemoveHandler(ucData.DataChangedEvent, value); } } 我怎么能用F#做呢 注意:在输出wpf测试窗口中,用户控件“ucData

在F#中,我无法执行请求对象中描述的操作

缺少的一点是C事件的F#实现,声明如下:

public event RoutedEventHandler DataChanged
{ 
 add { base.AddHandler(ucData.DataChangedEvent, value); }
 remove { base.RemoveHandler(ucData.DataChangedEvent, value); }
}
我怎么能用F#做呢

注意:在输出wpf测试窗口中,用户控件“ucData”起作用(属性和文本框之间已实现内部绑定),但当我更改“数据”属性“按代码”或“按窗口”的值时,位于测试wpf窗口内的“DataChanged”事件的侦听器不会触发/激发(使用wpf窗口内显示的用户控件上的键盘输入)

谢谢(一如既往)

用户控件“ucData”的属性:
静态let-OnDataPropertyChanged(发送方:DependencyObject)(e:DependencyPropertyChangedEventArgs)=
let control=unbox(发送方)
设newValue=unbox(例如newValue)
让oldValue=unbox(例如,oldValue)
System.Console.WriteLine
(斯普林特)

“>>>OnPropertyChanged'ucData':'Data':控件名称:%s;值:%f-->%f>>OnCerValue'ucData':'Data':控件名称:%s;值::%f OnValidateValue'ucData':'Data':数据%s这里有完整的功能C版本的“ucData”(f仍然存在C与f之间“事件”定义不同的问题):

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用系统组件模型;
命名空间库
{
/// 
///UserControl1.xaml的交互逻辑
/// 
公共部分类ucInput:UserControl
{
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
公共静态依赖属性DescriptionProperty;
公共静态从属属性DataProperty;
公共静态从属属性度量属性的单位;
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
公共静态只读路由事件描述ChangedEvent;
公共静态只读路由事件DataChangedEvent;
公共静态只读路由事件度量单位更改;
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
私有静态void OnDescriptionChanged(DependencyObject发送方,DependencyPropertyChangedEventArgs e)
{
ucInput控件=(ucInput)发送方;
字符串oldValue=(字符串)e.oldValue;
字符串newValue=(字符串)e.newValue;
字符串结果=string.Format(

“>>>OnPropertyChanged'ucInput':'Description':控件名称:{0};值:{1}-->{2}>>OnPropertyChanged'ucInput':'Data':控件名称:{0};值:{1}-->{2}>>OnPropertyChanged'ucInput':'UnitOfMeasure':控件名称:{0};值:{1}-->{2}>>OnCerValue'ucInput':'UnitOfMeasure:{0}”OnCercevalue'ucInput':'UnitOfMeasure':控件名称:{0};值:{1}OnCercevalue'ucInput':'UnitOfMeasure':控件名称:{0};值:{1}OnValidateValue'ucInput':'UnitOfMeasure':类型:{0}OnValidateValue'ucInput':'UnitOfMeasure':类型:{0}OnValidateValidateValue'ucInput':'UnitOfMeasure:'。。。
static let  OnDataPropertyChanged (sender:DependencyObject) (e:DependencyPropertyChangedEventArgs) =
    let control = unbox<ucData>(sender)
    let newValue = unbox<double>(e.NewValue)
    let oldValue = unbox<double>(e.OldValue)
    System.Console.WriteLine
        (sprintf 
            ">>> OnPropertyChanged 'ucData':'Data': Control Name: %s; Value: %f --> %f <<<" 
            control.Name oldValue newValue )
    let argsEvent = new RoutedPropertyChangedEventArgs<double>(oldValue, newValue)
    argsEvent.RoutedEvent <- ucData.DataChangedEvent  // I get an ERROR here!!!!
    control.RaiseEvent(argsEvent)


static let OnCoerceDataProperty (sender:DependencyObject) (data:obj) =
    let control = unbox<ucData>(sender)
    let value = unbox<double>(data)
    System.Console.WriteLine
        (sprintf 
            ">>> OnCoerceValue 'ucData':'Data': Control Name: %s; Value: : %f <<<" 
            control.Name value )
    box(value)

static let OnValidateDataProperty (data:obj) =
    System.Console.WriteLine
        (sprintf 
            ">>> OnValidateValue 'ucData':'Data': Data %s <<<" 
            (data.ToString()) )
    data.GetType() = typeof<double>

static let dpData = 
    DependencyProperty.Register("Data",typeof<double>, typeof<ucData>, 
        new FrameworkPropertyMetadata( 0.0,
            FrameworkPropertyMetadataOptions.Inherits,
            new PropertyChangedCallback(OnDataPropertyChanged),
            new CoerceValueCallback(OnCoerceDataProperty) ),
        new ValidateValueCallback(OnValidateDataProperty) )

static member DataProperty = dpData

[<Description("Specify the Numerical Data"); Category("UserData")>]
member x.Data
    with get() = 
        let res = x.GetValue(ucData.DataProperty) 
        (res :?> double)
    and set (v:double) = 
        x.SetValue(ucData.DataProperty, v )
static let reDataChangedEvent =
    EventManager.RegisterRoutedEvent
        ("DataChanged", RoutingStrategy.Bubble, 
            typeof<RoutedPropertyChangedEventHandler<double>>, typeof<ucData>)

let dataChangedEvent = 
    let e = new  Event<RoutedPropertyChangedEventHandler<double>,RoutedPropertyChangedEventArgs<double>>() 
    // Equialent to: 
    //    public event RoutedPropertyChangedEventHandler<double> DataChanged
    //    {
    //       add { AddHandler(DataChangedEvent, value); }
    //       remove { RemoveHandler(DataChangedEvent, value); }
    //    }
    // where DataChangedEvent is so defined:
    //    public static readonly RoutedEvent DataChangedEvent;        
    e

[<CLIEvent>]
static member DataChangedEvent with get() = reDataChangedEvent   // I get an ERROR here!!!

[<CLIEvent>]
member x.DataChanged = dataChangedEvent.Publish 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace UCLibrary
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class ucInput : UserControl
    {

        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

        public static DependencyProperty DescriptionProperty;

        public static DependencyProperty DataProperty;

        public static DependencyProperty UnitOfMeasureProperty;

        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

        public static readonly RoutedEvent DescriptionChangedEvent;

        public static readonly RoutedEvent DataChangedEvent;

        public static readonly RoutedEvent UnitOfMeasureChangedEvent;

        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

        private static void OnDescriptionChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e )
        {
            ucInput control = (ucInput)sender;
            string oldValue = (string)e.OldValue;
            string newValue = (string)e.NewValue;

            string result = string.Format(
                ">>> OnPropertyChanged 'ucInput':'Description': Control Name: {0}; Value: {1} --> {2} <<<",
                control.Name, 
                oldValue, 
                newValue);

            System.Console.WriteLine(result);

            RoutedPropertyChangedEventArgs<string> argsEvent =
                new RoutedPropertyChangedEventArgs<string>(oldValue, newValue);

            argsEvent.RoutedEvent = ucInput.DescriptionChangedEvent;

            control.RaiseEvent(argsEvent);

        }

        private static void OnDataChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ucInput control = (ucInput)sender;
            double oldValue = (double)e.OldValue;
            double newValue = (double)e.NewValue;

            string result = string.Format(
                ">>> OnPropertyChanged 'ucInput':'Data': Control Name: {0}; Value: {1} --> {2} <<<",
                control.Name,
                oldValue,
                newValue);

            System.Console.WriteLine(result);

            RoutedPropertyChangedEventArgs<double> argsEvent =
                new RoutedPropertyChangedEventArgs<double>(oldValue, newValue);
            argsEvent.RoutedEvent = ucInput.DataChangedEvent;

            control.RaiseEvent(argsEvent);

        }

        private static void OnUnitOfMeasureChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ucInput control = (ucInput)sender;
            string oldValue = (string)e.OldValue;
            string newValue = (string)e.NewValue;

            string result = string.Format(
                ">>> OnPropertyChanged 'ucInput':'UnitOfMeasure': Control Name: {0}; Value: {1} --> {2} <<<",
                control.Name,
                oldValue,
                newValue);

            System.Console.WriteLine(result);

            RoutedPropertyChangedEventArgs<string> argsEvent =
                new RoutedPropertyChangedEventArgs<string>(oldValue, newValue);
            argsEvent.RoutedEvent = ucInput.UnitOfMeasureChangedEvent;

            control.RaiseEvent(argsEvent);

        }

        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

        private static object OnCoerceDescriptionProperty(DependencyObject sender, Object data)
        {
            ucInput control = (ucInput)sender;
            string value = (string)data;

            value = value + ":";

            string result = string.Format(
                ">>> OnCoerceValue 'ucInput':'UnitOfMeasure': Control Name: {0}; Value: {1} <<<",
                control.Name,
                value);

            System.Console.WriteLine(result);

            return (object)value;

        }

        private static object OnCoerceDataProperty(DependencyObject sender, Object data)
        {
            ucInput control = (ucInput)sender;
            double value = (double)data;

            string result = string.Format(
                ">>> OnCoerceValue 'ucInput':'UnitOfMeasure': Control Name: {0}; Value: {1} <<<",
                control.Name,
                value);

            System.Console.WriteLine(result);

            return (object)value;

        }

        private static object OnCoerceUnitOfMeasureProperty(DependencyObject sender, Object data)
        {
            ucInput control = (ucInput)sender;
            string value = (string)data;

            value = "[" + value + "]";

            string result = string.Format(
                ">>> OnCoerceValue 'ucInput':'UnitOfMeasure': Control Name: {0}; Value: {1} <<<",
                control.Name,
                value);

            System.Console.WriteLine(result);

            return (object)value;

        }

        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

        private static bool OnValidateDescriptionProperty(Object data)
        {

            string result = string.Format(
                ">>> OnValidateValue 'ucInput':'UnitOfMeasure': TypeOf: {0} <<<",
                data.GetType().ToString());

            System.Console.WriteLine(result);

            return ( data.GetType() == typeof(String) );

        }

        private static bool OnValidateDataProperty(Object data)
        {

            string result = string.Format(
                ">>> OnValidateValue 'ucInput':'UnitOfMeasure': TypeOf: {0} <<<",
                data.GetType().ToString());

            System.Console.WriteLine(result);

            return (data.GetType() == typeof(Double));

        }

        private static bool OnValidateUnitOfMeasureProperty(Object data)
        {

            string result = string.Format(
                ">>> OnValidateValue 'ucInput':'UnitOfMeasure': TypeOf: {0} <<<",
                data.GetType().ToString());

            System.Console.WriteLine(result);

            return (data.GetType() == typeof(String));

        }

        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

        static ucInput()
        {

            // ------------------------------------------------------------------------------------------ //

            DescriptionProperty =
                DependencyProperty.Register(
                    "Description", typeof(string), typeof(ucInput),
                    new FrameworkPropertyMetadata("Description",
                           FrameworkPropertyMetadataOptions.Inherits,
                           new PropertyChangedCallback(OnDescriptionChanged),
                           new CoerceValueCallback(OnCoerceDescriptionProperty)),
                     new ValidateValueCallback(OnValidateDescriptionProperty)
                    );

            DataProperty =
                DependencyProperty.Register(
                    "Data", typeof(double), typeof(ucInput),
                    new FrameworkPropertyMetadata(0.0,
                           FrameworkPropertyMetadataOptions.Inherits,
                           new PropertyChangedCallback(OnDataChanged),
                           new CoerceValueCallback(OnCoerceDataProperty)),
                     new ValidateValueCallback(OnValidateDataProperty)
                    );

            UnitOfMeasureProperty =
                DependencyProperty.Register(
                "UnitOfMeasure", typeof(string), typeof(ucInput),
                new FrameworkPropertyMetadata("UM",
                           FrameworkPropertyMetadataOptions.Inherits,
                           new PropertyChangedCallback(OnUnitOfMeasureChanged),
                           new CoerceValueCallback(OnCoerceUnitOfMeasureProperty)),
                     new ValidateValueCallback(OnValidateUnitOfMeasureProperty)
                );

            // ------------------------------------------------------------------------------------------ //

            DescriptionChangedEvent = 
                EventManager.RegisterRoutedEvent(
                    "DescriptionChanged", RoutingStrategy.Bubble,
                    typeof(RoutedPropertyChangedEventHandler<string>), typeof(ucInput));

            DataChangedEvent =
                EventManager.RegisterRoutedEvent(
                    "DataChanged", RoutingStrategy.Bubble,
                    typeof(RoutedPropertyChangedEventHandler<double>), typeof(ucInput));

            UnitOfMeasureChangedEvent =
                EventManager.RegisterRoutedEvent(
                    "UnitOfMeasureChanged", RoutingStrategy.Bubble,
                    typeof(RoutedPropertyChangedEventHandler<string>), typeof(ucInput));

        }

        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

        public ucInput()
        {
            InitializeComponent();
        }

        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

        [Description("Description of the Numerical Data"), Category("UserData")]
        public string Description
        {
            get { return (string)GetValue(DescriptionProperty); }
            set { SetValue(DescriptionProperty, value); }
        }

        [Description("Specify the Numerical Data"), Category("UserData")]
        public double Data
        {
            get { return (double)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        [Description("Specify the 'Unit of Measure'"), Category("UserData")]
        public string UnitOfMeasure
        {
            get { return (string)GetValue(UnitOfMeasureProperty); }
            set { SetValue(UnitOfMeasureProperty, value); }
        }

        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

        [Description("Property 'Description' Changed"), Category("UserData")]
        public event RoutedPropertyChangedEventHandler<string> DescriptionChanged
        {
            add { AddHandler (DescriptionChangedEvent, value); }
            remove { RemoveHandler(DescriptionChangedEvent, value); }
        }

        [Description("Property 'Data' Changed"), Category("UserData")]
        public event RoutedPropertyChangedEventHandler<double> DataChanged
        {
            add { AddHandler(DataChangedEvent, value); }
            remove { RemoveHandler(DataChangedEvent, value); }
        }

        [Description("Property 'UnitOfMeasure' Changed"), Category("UserData")]
        public event RoutedPropertyChangedEventHandler<double> UnitOfMeasureChanged
        {
            add { AddHandler(UnitOfMeasureChangedEvent, value); }
            remove { RemoveHandler(UnitOfMeasureChangedEvent, value); }
        }

        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

    }
}
<UserControl x:Class="UCLibrary.ucInput"
         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:UCLibrary" 
         mc:Ignorable="d" 
         d:DesignHeight="30" d:DesignWidth="507">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100*" MinWidth="100"/>
        <ColumnDefinition Width="Auto" MinWidth="80"/>
        <ColumnDefinition Width="70*" MinWidth="70"/>
    </Grid.ColumnDefinitions>
    <Label Name="lblDescription" 
           Margin="3"
           Grid.Column="0"
           Content="{Binding Description, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ucInput}}}"/>
    <TextBox Name="txtData" 
             Margin="3"
             Grid.Column="1"
             Text="{Binding Data, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ucInput}}, StringFormat=N}">
    </TextBox>
    <Label Name="lblUnitOfMeasure" 
           Margin="3"
           Grid.Column="2"
           Content="{Binding UnitOfMeasure, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ucInput}}}"/>
</Grid>