在Wpf窗口中使用创建的DLL
本文关键字:创建 DLL Wpf 窗口 | 更新日期: 2023-09-27 18:26:43
我有一个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
}
}
以及带有以下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堆栈溢出,即WPF GUI和几个ListBox/CheckBox 之间的DataBinding
和其他人,但我做不好。尽管堆栈溢出帮助我在控制台应用程序中利用了这一点。现在我必须在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文件中添加文本块,将text绑定到属性:
<TextBlock Text="{Binding Path=netFrameworkAvailable}" ... />
其他一些评论:
- 你没有很好地利用<网格>要素最好定义ColumnDefinitions和RowDefinitions,创建一个2xn表
- ApCkr方法都可以是静态的。此类没有上下文
- 如果.net框架不可用,我认为您的应用程序无法运行。如果您的应用程序正在运行,您可以安全地将"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">
下面的最后一行是无效的。它决定您使用特定的dll。
xmlns:my="clr-namespace:TestWpfControlLibrary;assembly=TestWpfControlLibrary"`
之后,在网格teg中的xaml页面中使用define"my",在xmlns:my 中使用的是谁
用于以下用途的代码。
<my:UserControl1 Height="168" HorizontalAlignment="Left" Margin="10,22,0,0" VerticalAlignment="Top" Width="307" Name="login" />
之后转到form.cs页面,并在页面加载方法中编写以下代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
login.Visibility = System.Windows.Visibility.Visible;
}
}
现在你的dll工作正常。
你可以从下面的链接下载这样的测试应用程序
http://www.dotnetfoundation.somee.com/Style/DesktopApp/WPFTEST.zip