在Wpf窗口中使用创建的DLL?

时间:2012-03-06 05:57:10

标签: 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()
        {
            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

    }

}

带有followig XAML代码的WPF窗口

<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#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }

        private void textBlock1_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            var NET = new ApplicationCheck.ApCkr();
            textBlock1.Text = NET.Netframeworkavailable();
            this.DataContext = textBlock1;

        }


    }
}

我研究了MSDN和At堆栈溢出中的数据绑定,即 - DataBinding Between a WPF GUI and a couple of ListBox/CheckBox

和其他人,但我无法做到正确。虽然堆栈溢出帮助我在控制台应用程序中使用它。现在我必须在WPF窗口中执行此操作。

编辑:我必须显示DLL中返回的值

2 个答案:

答案 0 :(得分:1)

为了使用绑定在UI中呈现DLL中的数据,您需要拥有一个带有公共getter的对象。在你的UI DLL中用公共getter创建一个类(在mvvm设计patern中,这个类称为'View Model'):

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文件中添加文本块,将Text绑定到属性:

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

其他一些评论:

  • 你没有很好地利用&lt; Grid&gt;元件。你最好定义ColumnDefinitions和RowDefinitions,创建一个2xn表。
  • ApCkr方法都可以是静态的。这个类没有上下文。
  • 如果.net框架不可用,我认为您的应用程序无法运行。如果您的应用程序正在运行,您可以放心地将'true'放在那里。
  • 考虑在ApCkrVm中缓存值。

答案 1 :(得分:0)

首先在你的资源中添加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">

这最后一行是无能为力的。它决定你使用特定的DLL。

xmlns:my="clr-namespace:TestWpfControlLibrary;assembly=TestWpfControlLibrary"`

之后在网格teg中的xaml页面中使用define&#34; my&#34;,你在xmlns中使用的那些:我的

用于代码之后的使用。

<my:UserControl1 Height="168" HorizontalAlignment="Left" Margin="10,22,0,0"  VerticalAlignment="Top" Width="307" Name="login" />

之后转到form.cs页面并在pageload方法中编写以下代码

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            login.Visibility = System.Windows.Visibility.Visible;
        }
    }

现在你的dll工作正常。

您可以从以下链接

下载这样的测试应用程序

http://www.dotnetfoundation.somee.com/Style/DesktopApp/WPF TEST.zip