簡體   English   中英

如何在 C# Wpf UserControl 中使用 F# 對象/記錄

[英]How to use F# objects/records in C# Wpf UserControl

(新手問題)。 我的主要應用程序在 F# 中運行。 從 F# 應用程序中,我在單獨的后台線程上調用打印例程。 此打印例程在單獨的 C# 項目中使用以下 wpf xaml 用戶控件來格式化頁面。

我的問題是帳戶 object 在 F# 中定義並在 C# 中使用 - 除了它不起作用。

錯誤:“Account”類型與“IAccount”類型不兼容

反正有沒有讓這兩種語言一起玩得很好?

(FsXaml 有可能嗎?)

感謝您的任何幫助或建議。

TIA

F#

let workSheetHeader (a:Account) (encounterTime: Nullable<DateTime> ) = 
            let Insurance = "Self Pay"
            let firstEncounter,lastEncounter = getFirstAndLastEncounter(a,encounterTime)
            let account = {Account.birthDate = "11/30/1999"; lastName = "lastname"; firstName = "firstname"; chartNumber = 1776; doNotSee = false; mi ="i" }
            let ws =  new WorksheetHeader (account, firstEncounter, lastEncounter, Insurance)
            ws :> FrameworkElement


WPF XAML

<UserControl x:Class="StargateIX.PrintingAndReports.WorksheetHeader"
             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:StargateIX.PrintingAndReports"
             mc:Ignorable="d" 
             d:DesignHeight="70" d:DesignWidth="816">

    <UserControl.Resources>
        <!--Without the x:Key, this will style ALL the TextBlocks-->
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="FontSize" Value="12" />
        </Style>
        <Style x:Key="Title" TargetType="{x:Type TextBlock}">
            <Setter Property="FontSize" Value="14" />
            <Setter Property="TextAlignment" Value="Center" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Top" />
        </Style>
    </UserControl.Resources>
    <Grid>
        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top"  Margin="24,0" >
            <TextBlock  Text="address 1"  />
            <TextBlock  Text="address 2"     />
            <TextBlock  Text="address 3"  />
        </StackPanel>
        <StackPanel HorizontalAlignment="Right" VerticalAlignment="Top" Margin="24,0">
            <TextBlock  Text="Chart Number" Name="textblockChartNumber" />
            <TextBlock  Text="Bd: 4/15/1960" Name="textblockBirthDate"  />
            <StackPanel   Orientation="Horizontal">
                <!--Last Encounter Date-->
                <TextBlock Text="First Visit: " />
                <TextBlock Text="{Binding FirstEncounter}" />
            </StackPanel>
            <StackPanel   Orientation="Horizontal">
                <!--Last Encounter Date-->
                <TextBlock Text="Last Visit: " />
                <TextBlock Text="{Binding LastEncounter}" />
            </StackPanel>

        </StackPanel>

        <TextBlock
            Style="{StaticResource Title}" Name="textblockPatientName"
            Text="Test Name" Width="332" Margin="242,0,242,0" />

        <!--Today's Date-->
        <TextBlock x:Name="textblockTodaysDate" HorizontalAlignment="Left" Margin="368,33,0,0" Text="11/04/2015" VerticalAlignment="Top"/>

        <!--Insurance-->
        <TextBlock HorizontalAlignment="Left" Height="16" Margin="24,48,0,0" Text="{Binding Insurance}" VerticalAlignment="Top" Width="209"/>

    </Grid>
</UserControl>


Code Behind:

using System;
using System.Windows.Controls;

namespace StargateIX.PrintingAndReports
{

    public interface IAccount
    {
        string lastName { get; }
        string firstName { get; }
        string  mi { get; }
        string birthDate { get; }
        int chartNumber { get;}
        bool doNotSee { get;}
    }

    /// <summary>
    /// Interaction logic for WorksheetHeader.xaml
    /// </summary>
    public partial class WorksheetHeader : UserControl
    {
        public WorksheetHeader(IAccount account, DateTime? firstEncounter, DateTime? lastEncounter, string insurance)
        {
            DataContext = this;
            FirstEncounter = firstEncounter;
            LastEncounter = lastEncounter;
            Insurance = insurance;

            InitializeComponent();

        //    var model = new ServiceService(service);

            textblockTodaysDate.Text = DateTime.Today.ToShortDateString();
        //    textblockBirthDate.Text = $"Bd: {account.birthDate:D}";
        //    textblockChartNumber.Text = $"C#: {account.chartNumber}";
        //    textblockPatientName.Text = PatientNameService.ProperName(account);
        }
        public DateTime? FirstEncounter { get; private set; }
        public string Insurance { get; private set; }
        public DateTime? LastEncounter { get; private set; }
    }
}

#附錄:如下所示,我將F#中的定義和接口移到了自己的項目中,然后從C# XAML中引用了這個項目。 它現在至少可以編譯了。 這是新的定義(在它自己的項目中):

module Contracts =

    type IAccount =
       abstract member lastName : string with get
       abstract member firstName : string with get
       abstract member mi : string with get
       abstract member birthDate: string with get
       abstract member chartNumber: int with get
       abstract member doNotSee: bool with get  
     

    type Account = 
       {
            lastName: string
            firstName: string
            mi: string
            birthDate: string
            chartNumber: int
            doNotSee: bool
       } 
       interface IAccount with
                member this.birthDate with get() = this.birthDate
                member this.chartNumber with get() = this.chartNumber
                member this.doNotSee with get() = this.doNotSee
                member this.firstName with get() = this.firstName
                member this.lastName with get() = this.lastName
                member this.mi with get() = this.mi

有沒有其他方法,或者可能有更好的方法來做到這一點?

TIA

如果只暴露接口,不關心具體類型,也可以使用 object 表達式。

type IAccount =
    abstract member lastName : string with get
    abstract member firstName : string with get
  

 let createAccount fstName lstName =
    { new IAccount with
        member _.firstName = fstName
        member _.lastName = lstName
    }
        

實現接口的記錄也很好。 F# 強迫你有非循環依賴,所以提前計划。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM