c# – F#中的依赖属性默认值不匹配

c# – F#中的依赖属性默认值不匹配,第1张

概述我试图转换一个C#依赖属性,它将输入ComboBox的文本的最大长度限制为F#.该程序是一个MVVM程序,它使用F#作为模型和视图模型,使用C#作为视图.工作的C#代码是这样的: public class myComboBoxProperties { public static int GetMaxLength(DependencyObject obj) { 我试图转换一个C#依赖属性,它将输入ComboBox的文本的最大长度限制为F#.该程序是一个MVVM程序,它使用F#作为模型和视图模型,使用C#作为视图.工作的C#代码是这样的:
public class myComboBoxPropertIEs    {        public static int GetMaxLength(DependencyObject obj)        {            return (int)obj.GetValue(MaxLengthProperty);        }        public static voID SetMaxLength(DependencyObject obj,int value)        {            obj.SetValue(MaxLengthProperty,value);        }        // Using a DependencyProperty as the backing store for MaxLength. This enables animation,styling,binding,etc...        public static Readonly DependencyProperty MaxLengthProperty =            DependencyProperty.Registerattached("MaxLength",typeof(int),typeof(myComboBoxPropertIEs),new UIPropertyMetadata(OnMaxLengthChanged));        private static voID OnMaxLengthChanged(DependencyObject obj,DependencyPropertyChangedEventArgs args)        {            if (obj is ComboBox)            {                ComboBox comboBox = (ComboBox)obj;                comboBox.Loaded += (sender,e) =>                {                    TextBox textBox = comboBox.Template.Findname("PART_EditableTextBox",comboBox) as TextBox;                    if (textBox != null)                    {                        textBox.SetValue(TextBox.MaxLengthProperty,args.NewValue);                    }                };            }        }    }

F#代码是这样的:

type myComboBoxPropertIEs() =    static let OnMaxLengthChanged  (myobj1 : DependencyObject,args : DependencyPropertyChangedEventArgs)  =        let comboBox = myobj1 :?> ComboBox        comboBox.Loaded.Subscribe (fun _ ->                                     let textBox : TextBox = comboBox.Template.Findname("PART_EditableTextBox",comboBox) :?> TextBox                                     match textBox with                                    | null -> ()                                    |_ -> textBox.SetValue(TextBox.MaxLengthProperty,args.NewValue))    static let MaxLengthProperty = DependencyProperty.Registerattached("MaxLength",typeof<int>,typeof<myComboBoxPropertIEs>,new UIPropertyMetadata(OnMaxLengthChanged))    static member GetMaxLength (myobj : DependencyObject) = myobj.GetValue(MaxLengthProperty) :?> int    static member SetMaxLength (myobj : DependencyObject,value : int) = myobj.SetValue(MaxLengthProperty,value)

我遇到的问题是我得到的XAML错误是:

Default value type does not match type of property MaxLength

我究竟做错了什么?

解决方法 你可以试试这个
open System.windowsopen System.windows.Controlstype MyComboBoxPropertIEs() =  static let OnMaxLengthChanged  (myobj1 : DependencyObject) (args : DependencyPropertyChangedEventArgs) =    let comboBox = myobj1 :?> ComboBox    comboBox.Loaded.Add (      fun _ ->         let textBox : TextBox = comboBox.Template.Findname("PART_EditableTextBox",comboBox) :?> TextBox         match textBox with        | null -> ()        |_ -> textBox.SetValue(TextBox.MaxLengthProperty,args.NewValue)      )  static let MaxLengthProperty =     DependencyProperty.Registerattached(      "MaxLength",typeof<MyComboBoxPropertIEs>,UIPropertyMetadata(0,PropertyChangedCallback OnMaxLengthChanged)      )  static member GetMaxLength (myobj : DependencyObject) = myobj.GetValue(MaxLengthProperty) :?> int  static member SetMaxLength (myobj : DependencyObject,value)

与代码的主要区别在于UIPropertyMetadata(0,PropertyChangedCallback OnMaxLengthChanged)将OnMaxLengthChanged转换为PropertyChangedCallback.

但是,即使每次更改最大值,您也会订阅.Loaded.我怀疑你只想第一次订阅?

总结

以上是内存溢出为你收集整理的c# – F#中的依赖属性默认值不匹配全部内容,希望文章能够帮你解决c# – F#中的依赖属性默认值不匹配所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1246507.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存