- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试仅使用 F#(版本 12.0.30815.0 + Framework 4.5)创建“UserControl”“By-Code”,因为我更喜欢这种“方式”(C# 和 F# 之间没有混合)。
注意:我必须在输入表单中显示单个类的多个属性,因此此 UserControl 需要实现绑定(bind)功能以将“输出”连接到类的属性。
我尝试修改窗口内的数值(即 1200 或 400)或使用键盘上的 TAB 键在控件之间移动焦点,但似乎已锁定。
无法实现数值的更新。我只能删除一个或多个单个数字,没有任何更新。
也许我在 UserControls 中实现的事件“dataChangedEvent”中存在概念错误(即“ucData”控件的“Data”属性 - 请参阅代码)。它类似于:“INotifyPropertyChanged”。
注意:也许在事件实现中的这个错误也存在于 UserControl 的“Description”和“UnitOfMeasure”属性中。
有人可以帮我找出下面报告的代码行中我的错误在哪里吗?
非常感谢。
人工神经网络
type ucData() as this =
inherit Windows.Controls.UserControl()
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 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
let grid =
let c = new Grid()
let colData = new ColumnDefinition()
colData.MinWidth <- 70.
c.ColumnDefinitions.Add( colData )
c
let data=
let c = new TextBox()
c.Margin<- new Thickness(3.0)
c.SetValue(Grid.ColumnProperty,1)
c
do
grid.Children.Add(data) |> ignore
this.AddChild(grid) |> ignore
let bData = new Binding()
bData.Path <- new PropertyPath("Data")
bData.StringFormat <- "N"
bData.ConverterCulture <- System.Globalization.CultureInfo.InvariantCulture
bData.Mode <- BindingMode.TwoWay
bData.RelativeSource <- new RelativeSource(RelativeSourceMode.FindAncestor,typeof<ucData>,1)
data.SetBinding(TextBox.TextProperty, bData) |> ignore
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 )
[<CLIEvent>]
static member DataChangedEvent with get() = reDataChangedEvent // I get an ERROR here!!!
[<CLIEvent>]
member x.DataChanged = dataChangedEvent.Publish
type TestWindow() as this =
inherit Window()
let c1 = new ucData()
let c2 = new ucData()
do
c1.Name <- "Data1"
c2.Name <- "Data2"
this.Width <- 300.
this.Height <- 300.
let sp = new StackPanel()
c1.Data <- 1200.
c2.Data <- 400.
sp.Children.Add(c1) |> ignore
sp.Children.Add(c2) |> ignore
c1.DataChanged.Add( fun args ->
MessageBox.Show("The button labeled \"" + c1.Data.ToString()) |>ignore )
c2.DataChanged.Add( fun args ->
MessageBox.Show("The button labeled \"" + c1.Data.ToString()) |>ignore )
this.Content <- sp
let w = new TestWindow()
w.Show()
最佳答案
根据 this回答,你需要在 FSI 中启动一个 WPF 事件循环。由于年纪大了,我不得不稍微调整一下。这是您的脚本的完整工作版本:
#I @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5"
#r @"WindowsBase.dll"
#r @"PresentationCore.dll"
#r @"PresentationFramework.dll"
#r @"System.Xaml.dll"
#r @"UIAutomationProvider.dll"
#r @"UIAutomationTypes.dll"
module WPFEventLoop =
open System
open System.Windows
open System.Windows.Threading
open Microsoft.FSharp.Compiler.Interactive
open Microsoft.FSharp.Compiler.Interactive.Settings
type RunDelegate<'b> = delegate of unit -> 'b
let Create() =
let app =
try
// Ensure the current application exists. This may fail, if it already does.
let app = new Application() in
// Create a dummy window to act as the main window for the application.
// Because we're in FSI we never want to clean this up.
new Window() |> ignore;
app
with :? InvalidOperationException -> Application.Current
let disp = app.Dispatcher
let restart = ref false
{ new IEventLoop with
member x.Run() =
app.Run() |> ignore
!restart
member x.Invoke(f) =
try
disp.Invoke(DispatcherPriority.Send,new RunDelegate<_>(fun () -> box(f ()))) |> unbox
with e -> eprintf "\n\n ERROR: %O\n" e; reraise()
member x.ScheduleRestart() = ()
//restart := true;
//app.Shutdown()
}
let Install() = fsi.EventLoop <- Create()
WPFEventLoop.Install()
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
open System
open System.Windows
open System.Windows.Data
open System.Windows.Controls
open System.Windows.Input
open System.Windows.Media
open System.ComponentModel
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
type ucData() as this =
inherit Windows.Controls.UserControl()
// ---------------------------------------------------------------------------------------------------------------- //
static let dpDescription =
DependencyProperty.Register("Description", typeof<string>, typeof<ucData>)
static let dpData =
DependencyProperty.Register("Data", typeof<double>, typeof<ucData>)
static let dpUnitOfMeasure =
DependencyProperty.Register("UnitOfMeasure", typeof<string>, typeof<ucData>)
// ---------------------------------------------------------------------------------------------------------------- //
let descriptionChangedEvent = new Event<RoutedEventHandler, RoutedEventArgs>()
let dataChangedEvent = new Event<RoutedEventHandler, RoutedEventArgs>()
let unitOfMeasureChangedEvent = new Event<RoutedEventHandler, RoutedEventArgs>()
// ---------------------------------------------------------------------------------------------------------------- //
let grid =
let c = new Grid()
let colDesc = new ColumnDefinition()
colDesc.MinWidth <- 100.
colDesc.Width <- System.Windows.GridLength(100.,GridUnitType.Star)
c.ColumnDefinitions.Add( colDesc )
let colData = new ColumnDefinition()
colData.MinWidth <- 70.
colDesc.Width <- System.Windows.GridLength(70.,GridUnitType.Star)
c.ColumnDefinitions.Add( colData )
let colUnit = new ColumnDefinition()
colUnit.MinWidth <- 70.
colUnit.Width <- System.Windows.GridLength(70.,GridUnitType.Star)
c.ColumnDefinitions.Add( colUnit )
c
// ---------------------------------------------------------------------------------------------------------------- //
let desc =
let c = new Label()
c.Margin<- new Thickness(3.0)
c.SetValue(Grid.ColumnProperty,0)
c
// ---------------------------------------------------------------------------------------------------------------- //
let data=
let c = new TextBox()
c.Margin<- new Thickness(3.0)
c.SetValue(Grid.ColumnProperty,1)
c
// ---------------------------------------------------------------------------------------------------------------- //
let unit =
let c = new Label()
c.Margin<- new Thickness(3.0)
c.SetValue(Grid.ColumnProperty,2)
c
// ---------------------------------------------------------------------------------------------------------------- //
let condassign a b c =
match a with
| true -> b
| false -> c
// ---------------------------------------------------------------------------------------------------------------- //
do
grid.Children.Add(desc) |> ignore
grid.Children.Add(data) |> ignore
grid.Children.Add(unit) |> ignore
//this.Content <- grid |> ignore
this.AddChild(grid) |> ignore
// ---------------------------------------------------------------------------------------------------------------- //
this.Data <- 0.00
this.Description <- "<Description>"
this.UnitOfMeasure <- "<UM>"
// ---------------------------------------------------------------------------------------------------------------- //
this.DataContext <- this
// ---------------------------------------------------------------------------------------------------------------- //
let bDesc = new Binding()
bDesc.Path <- new PropertyPath("Description")
bDesc.Mode <- BindingMode.OneWay
bDesc.RelativeSource <- new RelativeSource(RelativeSourceMode.FindAncestor,typeof<ucData>,1)
let bData = new Binding()
bData.Path <- new PropertyPath("Data")
bData.Mode <- BindingMode.TwoWay
bData.RelativeSource <- new RelativeSource(RelativeSourceMode.FindAncestor,typeof<ucData>,1)
let bUnit = new Binding()
bUnit.Path <- new PropertyPath("UnitOfMeasure")
bUnit.Mode <- BindingMode.OneWay
//bUnit.RelativeSource <- new RelativeSource(RelativeSourceMode.FindAncestor,typeof<ucData>,1)
bUnit.Source <- this
// ---------------------------------------------------------------------------------------------------------------- //
desc.SetBinding(Label.ContentProperty, bDesc) |> ignore
data.SetBinding(TextBox.TextProperty, bData) |> ignore
unit.SetBinding(Label.ContentProperty, bUnit) |> ignore
// ---------------------------------------------------------------------------------------------------------------- //
static member DescriptionProperty = dpDescription
[<Description("Description of the Numerical Data"); Category("UserData")>]
member x.Description
with get() =
let res = x.GetValue(ucData.DescriptionProperty)
(res :?> string)
and set (v:string) =
x.SetValue(ucData.DescriptionProperty, (condassign (v=null) " " v) )
// ---------------------------------------------------------------------------------------------------------------- //
[<CLIEvent>]
member x.DescriptionChanged = descriptionChangedEvent.Publish
static member DescriptionChangedEvent =
EventManager.RegisterRoutedEvent
("DescriptionChanged", RoutingStrategy.Bubble,
typeof<RoutedEventHandler>, typeof<ucData>)
member x.OnDescriptionChangedEvent() =
let argsEvent = new RoutedEventArgs()
argsEvent.RoutedEvent <- ucData.DescriptionChangedEvent
argsEvent.Source <- x
descriptionChangedEvent.Trigger(this, argsEvent)
// ---------------------------------------------------------------------------------------------------------------- //
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 )
// ---------------------------------------------------------------------------------------------------------------- //
[<CLIEvent>]
member x.DataChanged = dataChangedEvent.Publish
static member DataChangedEvent =
EventManager.RegisterRoutedEvent
("DataChanged", RoutingStrategy.Bubble,
typeof<RoutedEventHandler>, typeof<ucData>)
member x.OnDataChangedEvent() =
let argsEvent = new RoutedEventArgs()
argsEvent.RoutedEvent <- ucData.DataChangedEvent
argsEvent.Source <- x
dataChangedEvent.Trigger(this, argsEvent)
// ---------------------------------------------------------------------------------------------------------------- //
static member UnitOfMeasureProperty = dpUnitOfMeasure
[<Description("Specify the 'Unit of Measure'"); Category("UserData")>]
member x.UnitOfMeasure
with get() =
let res = x.GetValue(ucData.UnitOfMeasureProperty)
(res :?> string)
and set (v:string) =
x.SetValue(ucData.UnitOfMeasureProperty, (condassign (v=null) " " v) )
// ---------------------------------------------------------------------------------------------------------------- //
[<CLIEvent>]
member x.UnitOfMeasureChanged = unitOfMeasureChangedEvent.Publish
static member UnitOfMeasureChangedEvent=
EventManager.RegisterRoutedEvent
("UnitOfMeasureChanged", RoutingStrategy.Bubble,
typeof<RoutedEventHandler>, typeof<ucData>)
member x.OnUnitOfMeasureChangedEvent() =
let argsEvent = new RoutedEventArgs()
argsEvent.RoutedEvent <- ucData.UnitOfMeasureChangedEvent
argsEvent.Source <- x
unitOfMeasureChangedEvent.Trigger(this, argsEvent)
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
type TestWindow() as this =
inherit Window()
let c1 = new ucData()
let c2 = new ucData()
do
this.Width <- 300.
this.Height <- 300.
let sp = new StackPanel()
c1.Description <- "Pippo"
c1.Data <- 1200.
c1.UnitOfMeasure <- "mm"
c1.BorderThickness <- new Thickness(2.0)
c2.Description <- "Pippo2"
c2.Data <- 400.
c2.UnitOfMeasure <- "MPa"
c2.BorderThickness <- new Thickness(2.0)
sp.Children.Add(c1) |> ignore
sp.Children.Add(c2) |> ignore
this.Content <- sp
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
let w = new TestWindow()
w.Show()
关于wpf - F# 以编程方式创建 UserControl : Missing Update (keyboard input seem locked),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28648356/
您好,我的 WPF UserControl 知识只有一个小时了。因此,如果有很多关于这个问题的教程或/和答案,请原谅我(老实说,我认为这无法完成,需要重新编写代码……因此我想问的原因) 因此,在创建
我看过几篇文章介绍如何删除在运行时添加的 UserControl,但我的问题有点不同。我有一个 UserControl,它由一个图像组成,右上角有一个小“x”按钮,用于从其父 Canvas 中删除自身
我有一个用户控件 UserControl1,它在其资源中定义了一种样式。该用户控件包含一个 UserControl2 实例,它引用了该样式:
我正在尝试以 How can a WPF UserControl inherit a WPF UserControl? 中提到的方式继承 WPF 中的用户控件 namespace DMS.Presen
我想创建几个具有一些通用功能的 WPF UserControl 类。出于这个原因,我想从一个新的基类派生类,而这个基类又派生自 UserControl。 我的问题是我的 C# 类是 部分 定义的,而自
这个问题在这里已经有了答案: Should a user control have its own view model? (6 个答案) How to correctly bind to a de
我有一个 UserControl,它包含另一个带有 Button 的 UserControl。我想在第一个 UserControl(父级)中向该按钮添加一个事件。我尝试这样做: void Page_I
我需要我所有的用户控件都具有一些类似的功能。所以我的用户控件是: using System.Windows.Controls; namespace WpfApplication26 { ///
以下 WPF UserControl 调用了 DataTypeWholeNumber,它有效。 现在我想创建一个名为 DataTypeDateTime 和 DataTypeEmail 等的 UserC
我有一个包含按钮和其他一些控件的 UserControl: ... 当我创建该控件的新实例时,我想获取 Button 的 Command 属性: 当然,“Th
我不确定这个问题是否已经发布,并且我已经检查了建议的 SO 链接,但似乎没有一个链接与该问题完全相关。因此,如果有人认为这可能是重复的,请发布建议答案的链接并原谅这篇文章。 解决我的问题的方法可能不止
我在 UserControl_1 中有两个 UserControl,有一个按钮可以在 MainWindow.axml 中的 StackPanel 中添加 UserControl_2。我在 UserCo
我正在开发一个由多个传感器组成的模块化项目,每个传感器都继承自一个抽象的 Sensor 类。为了配置每个传感器,我已经开始为每个传感器添加一个用户控制面板(继承自 UserControl),它在运行时
我正在使用 c#.net 我的网络表单中有不同的 View ,除了三个文本框(到达/看到/离开时间)之外,这些通常都显示不同的信息。为了尝试减少代码,我创建了一个包含这三个文本框的 UserContr
我有一个 UserControl,它有两个组件 public System.Windows.Forms.ComboBox innerComboBox; public System.Windows.Fo
我有一个包含 ListBox 的 userControl。我想从另一个 userControl 访问那个 ListBox。 例如: UserControl1.ListBox1.Items.Count;
我在更新另一个用户控件中的用户控件时遇到问题。 示例代码: UserControl MyCart1 = (UserControl)Page.FindControl("MyCart1"); Update
我在下面添加了一些屏幕截图和重现步骤。 我的数据模型有一个基类,我们称它为 CommonThing,它有很多属性。然后有几个具有附加属性的此类的特殊版本。我现在想创建一个 UI 来将数据输入到这个模型
我有一个应用程序,我需要确保在使用 ShowDialog() 单击用户控件上的按钮打开的表单将在我处理用户控件时关闭和处理。 我通过计时器在主窗体中调用 userControl.Dispose()。
我正在努力实现... 用户控件(MyRepeater) Control Start Control End 页面 Page Start Page Item Tem
我是一名优秀的程序员,十分优秀!