|
Thanks a lot 
|
|
|
|
|
Hi,
I'm working on extensibility of my Silverlight application and would like to load a dll on the client side and invoke methods based on an interface, can it be done? Any links to good articles?
I would prefer to not include it in my build of my application, ie the client can upload extensions, and then invoke them, so its not a referenced library...
____________________________________________________________
Be brave little warrior, be VERY brave
|
|
|
|
|
Adriaan Davel wrote: would like to load a dll on the client side and invoke methods based on an interface, can it be done?
Yes.
Given a Stream to an assembly DLL, you can use the
AssemblyPart.Load method to load the assembly into
the current app domain and get an Assembly object.
Then you can use Assembly.CreateInstance to create
instances of classes in the assembly.
Stream sourcestream = ...;
AssemblyPart assemblypart = new AssemblyPart();
Assembly assembly = assemblypart.Load(sourcestream);
IMyInterface myinterfaceobject = assembly.CreateInstance("MyNamespace.SomeClassThatInheritsFromIMyInterface") as IMyInterface;
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Mark,
Thanks for the reply. Loading the assembly and creating an instance is easy enough, my challenge is getting the assembly to the client side in the best possible way as it will not ship with the .xap file, any ideas around that?
____________________________________________________________
Be brave little warrior, be VERY brave
|
|
|
|
|
Adriaan Davel wrote: my challenge is getting the assembly to the client side in the best possible way as it will not ship with the .xap file, any ideas around that?
Maybe try downloading it like you would any resource.
For example, Using the WebClient class' OpenReadAsync() method
to download the assembly DLL, you'll get a stream in the
OpenReadCompleted event which you can use in the sample code
I posted.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I've played with OpenReadAsync and it works well, but it means that I have to download the assembly everytime, which I'm trying to avoid...
____________________________________________________________
Be brave little warrior, be VERY brave
|
|
|
|
|
Adriaan Davel wrote: it means that I have to download the assembly everytime, which I'm trying to avoid...
The XAP file is downloaded every time too, along with every
resource not included in the XAP file. Cached versions will be
used if available.
The only option you have without user intervention is
isolated storage.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks, right now I'm trying to understand MEF, else Isolated Storage is probably the right option...
____________________________________________________________
Be brave little warrior, be VERY brave
|
|
|
|
|
Here's a version that searches the assembly for the first
instance of a class that implements your interface, and if
found, creates an instance of that class:
IMyInterface myinterfaceobject = null;
Stream sourcestream = ...;
AssemblyPart assemblypart = new AssemblyPart();
Assembly assembly = assemblypart.Load(sourcestream);
foreach (Type t in assembly.GetTypes())
{
if (null != t.GetInterface("IMyInterface", false))
{
myinterfaceobject = assembly.CreateInstance(t.FullName) as IMyInterface;
break;
}
}
if (myinterfaceobject != null)
{
}
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I'm also looking at MEF for this, still not sure if it will work for this (or even how it works )
____________________________________________________________
Be brave little warrior, be VERY brave
|
|
|
|
|
Hi,
currently I'm testing a really small WPF app that uses resources
from external sources and at first it appeared to work fine. Now,
magically, it's not even able to load a resource from the same assembly.
The following code:
Uri uri = new Uri("thewinda.xaml", UriKind.Relative);
Window w = Application.LoadComponent(uri) as Window;
causes a XamlParseException with the message:
Cannot create instance of 'thewinda' defined in assembly 'WpfTemplateTry, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'thewinda.xaml' Line 1 Position 9.
One of its inner exceptions says:
The calling thread must be STA, because many UI components require this.
This is quite weird as "thewinda.xaml" is a file added to the same project as my application.
I've read this error can be connected with threading/async issues, but I have no idea,
how I could apply this hint to my case.
This is the body of "thewinda.xaml":
<Window x:Class="WpfTemplateTry.thewinda"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
</Window>
|
|
|
|
|
Are you doing the load on a separate thread?
What is the build type set to on the thewinda.xaml file?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks for your response.
I've managed to overcome the problem, but rather by changing my approach than by understanding what's actually wrong.
I was not loading the mentioned file on a separate thread explicitly. Maybe the system was doing something alike without letting me know.
Anyway, I tried to run that WPF application with a custom Main() method. (I didn't alter the auto-generated entry point file, but created my own basing on WinForms experience.) It was like that:
Main()
{
DerivedAppClass app = new DerivedAppClass();
app.Run();
}
DerivedAppClass was located in an external dll as an extension of the WPF's Application class. It's obvious, but seems to be not that straightforward in WPF. After I started following the default scheme(App.xaml + App.xaml.cs), it's working as expected.
|
|
|
|
|
New threads are MTA by default, but if you need STA you can
specify STA.
On your custom entry point function you can use
STAThreadAttribute:
[STAThread]
Main()
{
DerivedAppClass app = new DerivedAppClass();
app.Run();
}
On dynamically created threads you can use the SetApartmentState()
method:
mynewthread.SetApartmentState(ApartmentState.STA);
mynewthread.Start(...);
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Oh, excellent remark. LoadComponent() is working flawlessly from now on.
Thank you for your help and time.
|
|
|
|
|
I am really new for WPF, I am trying to create Radiobuttons,checkbox and textbox control at run time.
Is it possible to create controls at runtime in WPF,if yes then please let me know good link or sample code.
Thanks for taking your time!
I am using C# and VisualStudio2008.
|
|
|
|
|
Sr...Frank wrote: Is it possible to create controls at runtime in WPF
Of course. Elements declared in markup (XAML) are created
just like any other CLR objects.
Sr...Frank wrote: ,if yes then please let me know good link or sample code
using System.Windows.Controls;
...
RadioButton radiobutton = new RadioButton();
CheckBox checkbox = new CheckBox();
TextBox textbox = new TextBox();
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
How to bound them with windows form in WPF?
|
|
|
|
|
Sr...Frank wrote: How to bound them with windows form in WPF?
I'm not sure what you're asking here...
If you're using Windows Forms, why use WPF controls?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi,
Any trick to select multiple items in WPF listbox without holding shift key? Multiple items should be selected on holding and dragging just left mouse button.
Thanks
|
|
|
|
|
I'm learning that WPF is all about laying out your controls in a non-fixed fashion to be able to accomodate dynamic content. All well and good.
The application I need to write, however needs to display objects on a map in their precise locations. Does WPF provide enough support for fixed layout of objects so that I can implement my mapping application?
|
|
|
|
|
You can layout anything in a 'fixed' way by using a combination of the HorizontalAlighment VerticalAlignment and Margin properties, when used inside a Grid or a Canvas panel.
|
|
|
|
|
Hi..
I created a WPF C# application.
I deployed it to an computer with the .Net 3.5 framework.
When I double-click on the EXE nothing happens.. ANd i have got this error:
EventType : clr20r3 P1 : firstc1.exe P2 : 1.0.0.0
P3 : 4b2f37b0 P4 : presentationframework P5 : 3.0.0.0 P6 : 488f142e
P7 : 6259 P8 : e1 P9 : system.windows.markup.xamlparse.
And i have defined in App.xaml thsi resource for my Main Windows:
<Application x:Class="FirstC1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<SolidColorBrush x:Key="base_color" Color="#FF007D5A" />
<SolidColorBrush x:Key="selection" Color="#FF00422F" />
<Style x:Key="chart_border" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="{StaticResource base_color}"/>
<Setter Property="CornerRadius" Value="5"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush Opacity="0.25" StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="Transparent" Offset="0"/>
<GradientStop Color="{Binding Source={StaticResource base_color},Path=Color}" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
<!-- samples that can not be shown in XBAP -->
<Visibility x:Key="uiVisibilty">
Visible
</Visibility>
<Style TargetType="{x:Type TabItem}" x:Key="tree">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border
Name="Border"
BorderBrush="Black"
Background="Gold"
BorderThickness="1,1,1,1"
CornerRadius="25,5,0,0">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="12,0,12,0" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="White" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="LightGray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="FirstCell">
<StackPanel Orientation="Horizontal">
<CheckBox Name="chk1" IsChecked="{Binding Path=IsSelected,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>
</StackPanel>
</DataTemplate>
</Application.Resources>
</Application>
Thanks in advance.
|
|
|
|
|
And what have Visifire said?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
To use visifire, it's important to create a chart in a constructor or windows before use it in other function in a csharp code. I have so many errors in deploymnt of my application in WPF. Do you think in this case that it's an error in my app.xaml, specifically in a gradientbrush?
|
|
|
|