deception1
deception1

Reputation: 1767

Use created DLL in a Wpf window?

I have a DLL with the following code

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

    }

}

And a WPF window with the followig XAML code

<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>

and the WPF's c# code

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;

        }


    }
}

I researched data binding in MSDN and At stack overflow namely this - DataBinding Between a WPF GUI and a couple of ListBox/CheckBox

and others but i cannot get it right.And although stack overflow helped me utilise this in a console app.Now i have to do this in a WPF window.

Edit:I have to display the returned values from the DLL

Upvotes: 1

Views: 3883

Answers (2)

Jeetendra Negi
Jeetendra Negi

Reputation: 453

First add that Dll in your resources after that write following code in your wpf form xaml side.

<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">

this following last line is impotent. it decide you use the particular dll.

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

after that in xaml page within grid teg use define "my", whome you use in xmlns:my

for that use following code.

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

after that go to form.cs page and write the following code in pageload method

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

now your dll is working properly.

you can download a test application like this from following link

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

Upvotes: 0

Uri London
Uri London

Reputation: 10797

In order to present the data from the DLL in the UI using binding, you need to have an object with public getters. In your UI DLL create a class (in mvvm design patern, this class is called 'View Model') with the public getters:

public class ApCkrVm {
    public string netFrameworkAvailable {
        get { return ApCkr.NetFrameworkAvailable(); }
    }
    public string pdfAvailable {
        get { return ApCkr.PDFAvailable(); }
    }
    ...
}

Then, in Window2 constructor, set ApCkrVm to be the DataContext:

public Window2( ) {
    this.DataContext = new ApCkrVm( );
    InitializeComponent( );
}

Finally, add text blocks in the XML file, binding the Text to the properties:

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

Some other comments:

  • You aren't utilizing very well the <Grid> element. You'll be better off defining ColumnDefinitions and RowDefinitions, creating a 2xn table.
  • ApCkr method can all be static. This class has no context.
  • I don't think your application could run if .net framework isn't available. If your application is running, you can safely put 'true' there.
  • Consider caching the values in ApCkrVm.

Upvotes: 1

Related Questions