小编Ste*_*ing的帖子

在F#中的ViewBag动态对象上设置属性

我在C#中有这个动作方法:

  public ActionResult Index() {
      ViewBag.Message = "Hello";
      return View();
  }
Run Code Online (Sandbox Code Playgroud)

而这个观点(Index.cshtml):

  <h2>@ViewBag.Message</h2>
Run Code Online (Sandbox Code Playgroud)

这会在页面上产生预期的"Hello".

我想在F#中做控制器.我试过了

type MainController() =
  inherit Controller()
  member x.Index() =
    x.ViewBag?Message <- "Hello"
    x.View()
Run Code Online (Sandbox Code Playgroud)

这会产生错误消息"未找到方法或对象构造函数'op_DynamicAssignment'".

我查看了动态操作符的一些F#代码示例,我看不到任何比几页描述和许多代码行短的内容.它们似乎对于这个属性"setter"来说过于笼统.

f# razor asp.net-mvc-3

7
推荐指数
1
解决办法
1562
查看次数

将Action绑定到XAML中UserControl的属性

我有一个用户控件,它有一个按钮和一个依赖属性,用于执行按钮的操作.包含该控件的页面在XAML中设置操作.

MyUserControl.cs

一个按钮,依赖属性ButtonAction类型,Action.单击该按钮时,它将执行ButtonAction.

MainPage.xaml.cs中

行动1

行动2

MainPage.xaml中

MyUserControl使用ButtonAction = Action1呈现一个实例

问题:未从XAML分配ButtonAction属性

MyUserControl.cs

    public sealed partial class MyUserControl : UserControl
{

    public Action ButtonAction {
        get { return (Action)GetValue(ButtonActionProperty); }
        set { SetValue(ButtonActionProperty, value); }
    }

    public static readonly DependencyProperty ButtonActionProperty =
        DependencyProperty.Register("ButtonAction", typeof(Action), typeof(MyUserControl), new PropertyMetadata(null,ButtonAction_PropertyChanged));

    private static void ButtonAction_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        Debug.WriteLine("ButtonAction_PropertyChanged");
        // Is not called!
    }


    public MyUserControl() {
        this.InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e) { …
Run Code Online (Sandbox Code Playgroud)

windows-runtime winrt-xaml

5
推荐指数
1
解决办法
1097
查看次数

从F#函数构建Linq表达式,传递给C#

我正在尝试在WinRT 8.1应用程序中的F#项目中使用Lex.Db数据库.

我正在关注C#的本教程.我成功地将一个对Lex.Db的引用添加到了一个F#项目中,并且教程中的简单调用转换为f#并编译(例如let db = new DbInstance("demo")).

问题是这个C#代码:

db.Map<Contact>().Key(i => i.Id)
Run Code Online (Sandbox Code Playgroud)

- 编辑 -

为了避免其他人进一步阅读,在F#3.0中,这几乎不是问题.请参阅下面的kvb评论.解决方案是:

db.Map<Contact>().Key(fun i => i.Id)

- /编辑---

Key函数需要一个Expression<Func<Contact,'a>>,我无法在F#中构建它.

如何将LinQ表达式从F#传递到C#代码中出现了一个非常类似的问题.推荐的解决方案是使用LeafExpressionConverter.QuotationToLambdaExpression.

我试过这个如下:

 type Contact() =
    member val Id = 0 with get, set
    member val FirstName = "" with get, set

 let db = new DbInstance("Demo")

 let idExpr = LeafExpressionConverter.QuotationToLambdaExpression
                        <@ fun (c : Contact) -> c.Id @>

 db.Map<Contact>().Key(idExpr) |> ignore    // <- Error
Run Code Online (Sandbox Code Playgroud)

这会产生编译器错误idExpr …

f# c#-to-f# windows-runtime f#-3.0

4
推荐指数
1
解决办法
1027
查看次数

如何迭代枚举,并抛出obj

如何在F#中枚举枚举/类型告诉我们如何在F#中获取.Net枚举类型的枚举器:

使用: Enum.GetValues(typeof<MyType>)

但是,当我使用它时,我发现了一个限制.我可以解决这个限制,但我正在寻找一种更好的方法.

问题是该解决方案返回一个.Net数组对象,但要使用它我们需要转换它,并且该转换对于迭代来说是不实用的.

type Colors =
| Red = 0
| Blue = 1
| Green = 2

// Strongly typed function, to show the 'obj' in real use
let isFavorite color = color = Colors.Green

// Iterate over the Enum (Colors)
for color in Enum.GetValues(typeof<Colors>) do
    printfn "Color %A. Is Favorite -> %b" color (isFavorite color)  // <-- Need to cast
Run Code Online (Sandbox Code Playgroud)

(IsFavorite color)提高之间的冲突类型Colors和(预期)obj(实际)

这很容易解决:

for obj in Enum.GetValues(typeof<Colors>) …
Run Code Online (Sandbox Code Playgroud)

ienumerable enums f#

3
推荐指数
2
解决办法
885
查看次数

关闭包含UserControl的弹出窗口

我正在为TextBox构建一个用户控件,因为我希望它有一些特殊的行为.

该控件可用于多种上下文,包括作为按钮的弹出按钮.当它是弹出窗口时,我想在用户在编辑文本时按下Enter键时关闭弹出按钮.

文本框显示为弹出框,用户按回车键关闭弹出窗口![在此处输入图像说明

为实现此目的,控件具有ParentButton依赖项属性,如果设置,则将按钮与弹出按钮存储,并且父页面的XAML在此情况下设置它.控件有一个KeyUp处理程序,用于检测Enter键,如果ParentButton设置了属性,则关闭其弹出按钮.

TextBoxUC.xaml

<UserControl
x:Class="TextBoxUCDemo.TextBoxUC"
...
xmlns:local="using:TextBoxUCDemo"
...>

<StackPanel Width="250">
    <TextBox KeyUp="TextBox_KeyUp" Text="Hello" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

TextBoxUC.xaml.cs

    public sealed partial class TextBoxUC : UserControl
{
    public TextBoxUC() {
        this.InitializeComponent();
    }

    internal static readonly DependencyProperty ParentButtonProperty =
      DependencyProperty.Register("ParentButton", typeof(Button), typeof(TextBoxUC), new PropertyMetadata(null));

    public Button ParentButton {
        get { return ((Button)GetValue(ParentButtonProperty)); }
        set { SetValue(ParentButtonProperty, value); }
    }

    private void TextBox_KeyUp(object sender, KeyRoutedEventArgs e) {

        switch (e.Key) {
            case VirtualKey.Enter:

                // (Do something with the Text...) …
Run Code Online (Sandbox Code Playgroud)

windows-runtime winrt-xaml windows-store-apps

0
推荐指数
1
解决办法
1143
查看次数