Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET XAML WPF UI
Hi sir
 
Ok, I want loading any UserContols as DLL on a MainWindow project as host by selecting them in runtime.
I tried this article SelfContainedAssembly[^] and I converted it to a WPF project that you can see MyProject[^], also test it with StackPanel and etc succesfully, but I want develope a WPF UserControl project like this
<usercontrol x:class="ManyWpfControlLibrary.UserControl1" xmlns:x="#unknown">
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="300">
    <grid removed="#FFFA8383">
 
    </grid>
</usercontrol>
 
with code-behind
namespace ManyWpfControlLibrary
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
    }
}
 
This UserControl compiled to a DLL file, finally it would insert into my MainWindow project at runtime, but I couldn't to do this.
 
In fact, we haven't aware about the DLL (UserControl compiled), it created and developed offline by another persons and there is on a different assembly, only we load and insert it on the main project at runtime!
 
Help me about this method or another idea. If the issue is ambiguous and you got the idea, please you edit this post to more understanding.
 
Thanks in advance.
Posted 22-Nov-11 3:50am
hzawary1.5K
Edited 25-Nov-11 11:09am
v4
Comments
Amir Mahfoozi at 23-Nov-11 3:24am
   
I downloaded your project and commented all the code were in Window_Loaded and replaced "MainGrid.Children.Add(factory.GetUserControl());" with "MainGrid.Children.Add(new ManyWpfControlLibrary.UserControl1());" after adding a reference to ManyWpfControlLibrary and it works like a charm.
I know you want to implement factory pattern but this could be considered as an approach. Good Luck ;)
hzawary at 23-Nov-11 6:36am
   
Thanks Amir for doing it, but I want implement it exactly!
hzawary at 23-Nov-11 6:58am
   
In fact, we have not aware about the DLL (UserControl compiled), only we load it from the diffrent assembly and insert it on the main project at runtime!
Amir Mahfoozi at 23-Nov-11 7:04am
   
I got the idea, I will try more but first tell me about AssemblyLoader namespace that uses reflection to load assemblies. Is it yours or you have downloaded it from somewhere ? If so please give me their homepage to see their documents...
hzawary at 23-Nov-11 7:17am
   
Thanks again, wish you see the issue carefully.
 
http://www.codeproject.com/KB/miscctrl/SelfContainedAssembly.aspx
hzawary at 23-Nov-11 8:22am
   
Dear Amir, If you understand the issue me, so edit my post if is necessary;)
hzawary at 23-Nov-11 12:46pm
   
I am waiting for your response. what was the result?
hzawary at 25-Nov-11 17:10pm
   
Finally was solved:-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Something like this:
 
MyControl control = new MyControl();
this.rootGrid.Children.Add(control);
  Permalink  
Comments
hzawary at 22-Nov-11 10:07am
   
It doesn't look comfortable! you see my problem carefully and test MyProject please.
John Simmons / outlaw programmer at 22-Nov-11 11:00am
   
A lot of stuff you have to do in programming isn't comfortable. That doesn't make it "wrong". The solution i gave you is a GENERAL method for accomplishing what you orginally asked. Of course, requirements are going to dictate how you implement it in your own code, but that's not my concern.
hzawary at 22-Nov-11 11:45am
   
Thanks, but see you this article? http://www.codeproject.com/KB/miscctrl/SelfContainedAssembly.aspx, I've used it, only convert the project to WPF, problem is clearly.
SAKryukov at 22-Nov-11 17:10pm
   
This is irrelevant to the problem. The solution of John is correct and has nothing uncomfortable.
Actually, all controls are added like this; there is no such thing as "non-dynamic" control; the Designer does exactly this.
 
You are missing something. And John's answer gets my 5.
--SA
hzawary at 23-Nov-11 6:32am
   
Yes, and I didn't tell that the solution is incorrect, but I can not use it in the project! I want loading the any DLLs compiled (xaml+code-behind) on the main project at runtime.
   
That's not a reason to vote it a 1.
hzawary at 23-Nov-11 9:28am
   
Yes, the method is not related with the issue.
Amir Mahfoozi at 22-Nov-11 10:37am
   
+5 I think this is the correct solution. But sometimes it needs to be combined with some reflection methods...
John Simmons / outlaw programmer at 22-Nov-11 11:01am
   
What needs to be combined with my solution isn't something I can tell the OP. Only HE knows the specifics of his project, and only HE can implement the necessary code.
hzawary at 22-Nov-11 11:55am
   
You don't any idea yourself, why put comment?
You're a person that only vote up to rest posts.
Amir Mahfoozi at 22-Nov-11 14:31pm
   
Believe me or not I was going to give the same solution as John Simmons have given. And I did the same thing in my last project with use of reflection so I know what I say very well. And I always upvote because even a simple link have taken a persons time to help another person, so downvote have no meaning for me (Exception is when some people attack without any reason to others).
SAKryukov at 22-Nov-11 17:15pm
   
You are right. The attack from OP is very inappropriate, please see my comments.
--SA
Amir Mahfoozi at 23-Nov-11 0:15am
   
I think he thought that if I upvote an answer, no one will try to solve his problem but there are a lot of questions here which have many proposed solutions and I wish he can solve his problem very soon.
SAKryukov at 22-Nov-11 17:13pm
   
How can you be so hostile? People help you to overcome your own limitations in understanding things (sic!), and you are resisting, nothing else. And who is the looser here?
--SA
SAKryukov at 22-Nov-11 17:11pm
   
Again, Reflection is irrelevant here. The solution is correct.
--SA
hzawary at 25-Nov-11 17:10pm
   
Finally was solved:-)
SAKryukov at 22-Nov-11 17:11pm
   
My 5. Ridiculous arguments from OP!
--SA
hzawary at 25-Nov-11 17:17pm
   
Finally was solved:-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Ok guys, finally! my problem was solvedSmile | :)
 
1) Open AssemblyInfo.cs file of ManyWpfControlLibrary.csproj .
2) Add following attribute to this file[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
3) Build this project to get new assembly with the attribute.
4) Create *.zip from this new assembly
5) Delete older DependancyAssemblies.zip from WpfUserControlFactory.csproj
6) Add newly created zip in step 4 as embedded resource in WpfUserControlFactory.csproj.
  Permalink  
Comments
Amir Mahfoozi at 26-Nov-11 7:08am
   
Hi I am glad to see your problem solved but I have solved it with some another anti factory pattern :
private void Button_Click(object sender, RoutedEventArgs e)
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
@"\plugins\WpfUserControlFactory.dll";
 
Assembly testAssembly = Assembly.LoadFile(path);
Type pluginType = testAssembly.GetType("WpfUserControlFactory.AssisTouchScreen");
object pluginInstance = Activator.CreateInstance(pluginType);
UserControl nuc = (UserControl)pluginType.InvokeMember("GetUserControl", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, pluginInstance, null);
MainGrid.Children.Add(new ManyWpfControlLibrary.UserControl1());
 
}
hzawary at 27-Nov-11 12:34pm
   
Vvvvvvvvvery good;) very thanks I tried and it works like a charm!
with this
private void Button_Click(object sender, RoutedEventArgs e)
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\plugins\WpfUserControlFactory.dll";
Assembly testAssembly = Assembly.LoadFile(path);
Type pluginType = testAssembly.GetType("WpfUserControlFactory.AssisTouchScreen");
object pluginInstance = Activator.CreateInstance(pluginType);
UserControl nuc = (UserControl)pluginType.InvokeMember(
"GetUserControl",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null,
pluginInstance,
null);
MainStackPanel.Children.Add(nuc);
}
 
I have another a bit problem, there is way to disposing the dll? because I want update this dll file at in runtime, but when load this dll, cannot remove or modify this file, may you help me please also about this??
hzawary at 27-Nov-11 12:11pm
   
Thanks Amir, but this is wrong
MainGrid.Children.Add(new ManyWpfControlLibrary.UserControl1());
because there isn't the ManyWpfControlLibrary reference in the DemoClientWpfApp project!
we have only use AbstractWpfUserControlFactory and AssemblyLoader references in its!
So maybe this is your purpose MainGrid.Children.Add(new nuc);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 540
1 Sergey Alexandrovich Kryukov 407
2 Yogesh Kumar Tyagi 384
3 Prakriti Goyal 280
4 Maciej Los 205
0 OriginalGriff 6,632
1 Sergey Alexandrovich Kryukov 5,429
2 Maciej Los 3,474
3 Peter Leow 3,309
4 DamithSL 2,495


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 25 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100