C# 是否在Wpf窗口中使用创建的DLL?

C# 是否在Wpf窗口中使用创建的DLL?,c#,wpf,visual-studio-2010,xaml,C#,Wpf,Visual Studio 2010,Xaml,我有一个带有以下代码的DLL using System.Text; using Microsoft.Win32; using System.Diagnostics; using System.Runtime.InteropServices; namespace ApplicationCheck { public class ApCkr { #region .NET public string Netframeworkavailable()

我有一个带有以下代码的DLL

using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ApplicationCheck
{
    public  class ApCkr
    {

        #region .NET
        public string Netframeworkavailable()
        {
            bool NETinstall;
            RegistryKey k1 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Client");
            if (k1 == null)
            {
                NETinstall = false;
            }
            else
            {
                NETinstall = true;
            }
            return NETinstall.ToString();
        }


        #endregion

        #region PDF
        public string PDFavailable()
        {
            bool PDFinstall;
            RegistryKey k2 = Registry.ClassesRoot.OpenSubKey(".pdf");
            if (k2 == null)
            {
                PDFinstall = false;
            }
            else
            {
                PDFinstall = true;
            }
            return PDFinstall.ToString(); 
        }
        #endregion


        #region IExplore

        public string IEavailable()
        {

            bool IEversion;
            string  k3 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Internet Explorer").GetValue("Version").ToString();
            string z = k3.Substring(0, 1);
            int a = Int32.Parse(z);

             if (a < 8)
            {
                IEversion = false;
            }
            else
            {
                IEversion = true;
            }
            return IEversion.ToString();
        }
        #endregion


        #region IIS
        public string IISavailable()
        {
            bool IISinstall;
            RegistryKey k4 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\InetStp");
            if (k4 == null)
            {
                IISinstall = false;
            }
            else
            {
                IISinstall = true;
            }
            return IISinstall.ToString();
        }


        #endregion

    }

}
使用System.Text;
使用Microsoft.Win32;
使用系统诊断;
使用System.Runtime.InteropServices;
命名空间应用程序检查
{
公共类ApCkr
{
#区域.NET
公共字符串Netframeworkavailable()
{
bool-NETinstall;
RegistryKey k1=Registry.LocalMachine.OpenSubKey(“软件\\Microsoft\\NET框架设置\\NDP\\v4\\Client”);
如果(k1==null)
{
NETinstall=false;
}
其他的
{
NETinstall=true;
}
返回NETinstall.ToString();
}
#端区
#区域PDF
公共字符串PDFavailable()
{
布尔·芬斯塔尔;
RegistryKey k2=Registry.ClassesRoot.OpenSubKey(“.pdf”);
如果(k2==null)
{
PDFinstall=false;
}
其他的
{
PDFinstall=true;
}
返回PDFinstall.ToString();
}
#端区
#区域探索
公共字符串可用()
{
bool-IEversion;
字符串k3=Registry.LocalMachine.OpenSubKey(“软件\\Microsoft\\Internet Explorer”).GetValue(“版本”).ToString();
字符串z=k3。子字符串(0,1);
inta=Int32.Parse(z);
if(a<8)
{
IEversion=false;
}
其他的
{
IEversion=true;
}
返回IEversion.ToString();
}
#端区
#区域II
公共字符串IISavailable()
{
布尔·伊辛斯塔尔;
RegistryKey k4=Registry.LocalMachine.OpenSubKey(“软件\\Microsoft\\InetStp”);
if(k4==null)
{
IISinstall=false;
}
其他的
{
IISinstall=true;
}
返回IISinstall.ToString();
}
#端区
}
}
和一个WPF窗口,其中包含以下XAML代码

<Window x:Class="WpfApplication1.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        ResizeMode="CanResizeWithGrip"
        WindowStyle="None" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
        Title="Window2" Height="350" Width="525">
    <Grid>
        <Label Content="Windows" Height="25" HorizontalAlignment="Left" Margin="12,15,0,0" Name="label1" VerticalAlignment="Top" Width="106" />
        <Label Content="Edition " Height="25" HorizontalAlignment="Left" Margin="12,45,0,0" Name="label2" VerticalAlignment="Top" Width="106" />
        <Label Content="Service Pack " Height="25" HorizontalAlignment="Left" Margin="12,75,0,0" Name="label3" VerticalAlignment="Top" Width="106" />
        <Label Content="Version " Height="25" HorizontalAlignment="Left" Margin="12,105,0,0" Name="label4" VerticalAlignment="Top" Width="106" />
        <Label Content="Processor Bits " Height="25" HorizontalAlignment="Left" Margin="12,135,0,0" Name="label5" VerticalAlignment="Top" Width="106" />
        <Label Content="OS Bits " Height="25" HorizontalAlignment="Left" Margin="12,165,0,0" Name="label6" VerticalAlignment="Top" Width="106" />
        <Label Content="Program Bits " Height="25" HorizontalAlignment="Left" Margin="12,195,0,0" Name="label7" VerticalAlignment="Top" Width="106" />
        <TextBlock Height="21" HorizontalAlignment="Left" Margin="114,19,0,0" Name="textBlock1"   Text="{Binding Path=var}" VerticalAlignment="Top" Width="249" ContextMenuOpening="textBlock1_ContextMenuOpening" />
    </Grid>
</Window>

还有WPF的c#代码

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
命名空间WpfApplication1
{
/// 
///Window2.xaml的交互逻辑
/// 
公共部分类Window2:Window
{
公共窗口2()
{
初始化组件();
}
私有void textBlock1_ContextMenuOpening(对象发送方,ContextMenuEventArgs e)
{
var NET=newapplicationcheck.ApCkr();
textBlock1.Text=NET.Netframeworkavailable();
this.DataContext=textBlock1;
}
}
}
我研究了MSDN和堆栈溢出中的数据绑定,即:

虽然堆栈溢出帮助我在控制台应用程序中使用了它,但现在我必须在WPF窗口中使用它


编辑:我必须显示从DLL返回的值,为了使用绑定在UI中显示来自DLL的数据,您需要有一个带有公共getter的对象。在UI DLL中,使用公共getter创建一个类(在mvvm设计模式中,该类称为“视图模型”):

public class ApCkrVm {
    public string netFrameworkAvailable {
        get { return ApCkr.NetFrameworkAvailable(); }
    }
    public string pdfAvailable {
        get { return ApCkr.PDFAvailable(); }
    }
    ...
}
然后,在Window2构造函数中,将ApCkrVm设置为DataContext:

public Window2( ) {
    this.DataContext = new ApCkrVm( );
    InitializeComponent( );
}
最后,在XML文件中添加文本块,将文本绑定到属性:

<TextBlock Text="{Binding Path=netFrameworkAvailable}" ... />

其他一些评论:

  • 您没有很好地利用该元素。您最好定义ColumnDefinitions和RowDefinitions,创建一个2xn表
  • ApCkr方法都可以是静态的。这个类没有上下文
  • 如果.net framework不可用,我认为您的应用程序无法运行。如果您的应用程序正在运行,您可以安全地将“true”放在那里
  • 考虑在ApCkrVm中缓存值

为了使用绑定在UI中显示DLL中的数据,您需要有一个带有公共getter的对象。在UI DLL中,使用公共getter创建一个类(在mvvm设计模式中,该类称为“视图模型”):

public class ApCkrVm {
    public string netFrameworkAvailable {
        get { return ApCkr.NetFrameworkAvailable(); }
    }
    public string pdfAvailable {
        get { return ApCkr.PDFAvailable(); }
    }
    ...
}
然后,在Window2构造函数中,将ApCkrVm设置为DataContext:

public Window2( ) {
    this.DataContext = new ApCkrVm( );
    InitializeComponent( );
}
最后,在XML文件中添加文本块,将文本绑定到属性:

<TextBlock Text="{Binding Path=netFrameworkAvailable}" ... />

其他一些评论:

  • 您没有很好地利用该元素。您最好定义ColumnDefinitions和RowDefinitions,创建一个2xn表
  • ApCkr方法都可以是静态的。这个类没有上下文
  • 如果.net framework不可用,我认为您的应用程序无法运行。如果您的应用程序正在运行,您可以安全地将“true”放在那里
  • 考虑在ApCkrVm中缓存值

首先在资源中添加该Dll,然后在wpf表单xaml端编写以下代码

<Window x:Class="TestWpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="220" Width="343"
        xmlns:my="clr-namespace:TestWpfControlLibrary;assembly=TestWpfControlLibrary" Left="Auto" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None">
之后,在网格teg内的xaml页面中,使用定义“我的”,在xmlns中使用谁:我的

为此,请使用以下代码

<my:UserControl1 Height="168" HorizontalAlignment="Left" Margin="10,22,0,0"  VerticalAlignment="Top" Width="307" Name="login" />
现在您的dll工作正常

您可以从以下链接下载这样的测试应用程序


首先在资源中添加该Dll,然后在wpf表单xaml端编写以下代码

<Window x:Class="TestWpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="220" Width="343"
        xmlns:my="clr-namespace:TestWpfControlLibrary;assembly=TestWpfControlLibrary" Left="Auto" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None">
之后,在网格teg内的xaml页面中,使用定义“我的”,在xmlns中使用谁:我的

为此,请使用以下代码

<my:UserControl1 Height="168" HorizontalAlignment="Left" Margin="10,22,0,0"  VerticalAlignment="Top" Width="307" Name="login" />
现在您的dll工作正常

您可以从以下链接下载这样的测试应用程序


您需要了解WPF中的DataContext和绑定概念。这将帮助你实现你所追求的目标。在MSDN上查找它。您需要了解DataContext a