load silverlight controls in asp.net page - silverlight

I have a silverlight project which contains 2 silverlight controls Control_1 and Control_2. Please note that its in same app.Now I have asp.net project which will use any of these silverlight control (either Control_1 or Control_2).
Challenge is how do I tell silverlight which control to load. I used param property in html object to pass the parameters and tell the app which control to load at runtime?
But what if there are more than 2 controls in same project? We cannot have a long switch case statement in the app file only to load the controls. Is there any better way?

No, There isn't, and this is not about Silverlight itself, this is normal logic.
In your App.xaml file, put this:
using System.Windows; // Application, StartupEventArgs
namespace SilverlightApplication
{
public partial class App : Application
{
public App()
{
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
// Specify the main application UI
if(SomeCondition == true)
this.RootVisual = new Control1();
else
this.RootVisual = new Control2();
// In the same way, you may define a switch statment
}
}
}
You may decide what that condition is by passing parameters to the XAP file, and finally you access those by accessing e.InitParams in Application_Startup
For more info: Application.RootVisual

Related

How to "restore" SynchronizationContext when using custom startup?

My app works fine, but now I wanted to have custom startup, so I could catch any errors, add my logging from the start, etc.
So I used approach as shown in this answer What code controls the startup of a WPF application?
[STAThread]
public static void Main(string[] args) {
//include custom startup code here
var app = new MyApplication();//Application or a subclass thereof
var win = new MyWindow();//Window or a subclass thereof
app.Run(win); //do WPF init and start windows message pump.
}
It works fine with one exception -- SynchronizationContext.Current is null. And I need it :-)
So how to correctly make custom startup and have synchronization context?
Don't create your own Main method. Override the OnStartup method in your App.xaml.cs file:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var win = new MyWindow();
win.Show();
}
}
Then you will get a SynchronizationContext as usual.
Don't forget to remove the StartupUri attribute from the <Application> root element of your App.xaml file.

Application class in winforms and WPF

I am new to GUI development, i was trying to develop a sample ui application using winforms and WPF.
I found some of the code missing in WPF
namespace WindowsFormsApplication3
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Why this code is not present in WPF when the project is created
Why we are using Application class, what is the need of this class?
WPF uses the Application.Xaml and Application.cs to start your application.
It start your application and you can override some methods and choose how to start your application, apply come general configuration and error handling for your application
This code is not present because in the app.xaml definition there is usually a StarupURI="window1.xaml" attribute that allow the WPF infrastructure to wire the startup code behind the scene.
Of course you can create a custom bootsrapper, you need to remove the StartupURI attribute and intercept the Application startup to create/show a window:
public partial class App
{
protected override void OnStartup(StartupEventArgs e)
{
try
{
var mainView = new MainView();
mainView.Show();
mainView.DataContext = new YourDataContext();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}

Where put bootstrapper?

I want to create PRISM application with MVVM pattern and I don't know where I should put bootstrapper?
In Model, ViewModel or View?
Bootstrapper creates shell (so in View?) but it also registers container etc so maybe it should be like separate service?
The bootstrapper is part of the executable framework for configuring your application.
I suggest putting the bootstrapper code in the OnStartup event handler of your Application class.
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
SplashScreen splash = new SplashScreen("Resources\\mysplash.png");
splash.Show(true);
base.OnStartup(e);
MyBootstrapper b = new MyBootstrapper();
b.Run();
}
}
Technically, it is part of the View layer, imho, but is really there to configure the catalog and perform start-up operations.

Searching workaround to apply Localization on Ribbon Control using LocalizationExtension from codeplex

My application has to support multiple languages and should be able to switch the language on run time. For that purpose I am using LocalizationExtension from codeplex (http://wpflocalizeextension.codeplex.com/) I am using Ribbon Contorl in my application. I am creating ribbonCommands as window resource and Binding the LableTitle and other properties withLocalizationExtension class.
<MvvmCore:RibbonCommandExtended x:Key="SwitchLanguageCommand"
CanExecute="RibbonCommandExtended_CanExecute"
Executed="RibbonCommandExtended_Executed"
LabelTitle="{lex:LocText Key=SwitchLanguage,Dict=LanRes}"
ToolTipTitle="{lex:LocText Key=SwitchLanguage,Dict=LanRes}"
LargeImageSource="{lex:LocImage Key=ChangeLanguage,Dict=LanRes}"/>
Then assigning it to button Command property as static resource.
<rb:RibbonButton x:Name="EnglishButton" Command="{StaticResource SwitchToEnglishCommand}" Click="EnglishButton_Click">
Here is my RibbonCommandExtended class.
public class RibbonCommandExtended : RibbonCommand
{
private ICommand m_command;
public ICommand Command
{
get { return m_command; }
set
{
m_command = value;
if (m_command != null)
{
this.CanExecute += UsCanExecute;
this.Executed += UsExecuted;
}
else
{
this.CanExecute -= UsCanExecute;
this.Executed -= UsExecuted;
}
}
}
private void UsExecuted(object sender, ExecutedRoutedEventArgs e)
{
Command.Execute(e.Parameter);
}
private void UsCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = Command.CanExecute(e.Parameter);
}
}
When my program starts, then the ribbon control picks the right language strings and images. But when I change the language on runtime then I could't see any change in ribbon control localized text and image. Because the RibbonCommand's LabelTitle, LargeImageSource and all other properties are not Dependency properties.
Have someone solved the issue already? Or is there any other way rather then LocalizationExtension to make my application localized so that it fulfills my requirements?
It is easy to use the LocalizationExtension to localize the application. But perhaps, we should back to the basic method to do the localization, separated culture resource and the change it on the run-time. Please refer to the http://msdn.microsoft.com/en-us/library/ms788718.aspx. You may need the Locbaml tool to generate the CVS that we can edit it for several culture, and then load the CSV to the resource dll file for different culture, and change it by the code:
Thread.CurrentThread.CurrentCulture = new CultureInfo(...);
The following project provides a guidance about WPF localization - WPF Localization Guidance Whitepaper may help you: http://wpflocalization.codeplex.com/

Cannot show up WPF application when setting MainWindow manually and composing application (MEF)

I got my hands om MEF for a week now and I am trying to build up a WPF application that loads imported controls from MEF.
I created a WPF application project and removed the default window and application start up URI. Then I handled the application startup event to compose the application:
public partial class App : Application, IPartImportsSatisfiedNotification
{
{...}
private void App_Startup(object sender, StartupEventArgs e)
{
this.Compose();
}
public void Compose()
{
try
{
globalCatalog.Catalogs.Add(new DirectoryCatalog(extensionsDirectoryPath));
CompositionContainer container = new CompositionContainer(globalCatalog);
container.ComposeParts(this);
}
catch (Exception ex)
{
// Do something
}
}
{...}
}
Actually, when debugging and watching objects after imports are satisfied, everything has hierarchically composed fine like I wanted. But when I try to show up the MainWindow of the application an exception is thrown on MainWindow.Show() call:
"Specified element is already the logical child of another element. Disconnect it first."
Though my code in OnImportsSatisfied method seems fine as it is working when not using MEF mecanism:
public void OnImportsSatisfied()
{
Window mainWindow = new Window();
mainWindow.Content = this.importedControl;
this.MainWindow = mainWindow;
this.MainWindow.Show();
}
I insist on the fact that this works perfectly when not importing controls with MEF. What is surprising is that this code does not work too:
Window mainWindow = new Window();
//mainWindow.Content = this.importedControl;
this.MainWindow = mainWindow;
this.MainWindow.Show();
So I suspect that ComposeParts is doing a bit more than what it says as it is the only member acting on my actual application instance.
Hope someone can help me (Glenn?).
Thanks.
Edit:
I discovered that when I remove the IPartImportsSatisfiedNotification interface from my parts, no exception is thrown and the window shows up. But of course the window is empty as I need this OnImportsSatisfied method to set the DataContext of the window to its associated imported view model.
The sample applications of the WPF Application Framework (WAF) show how to use MEF within a WPF application.
I finally discovered that I was importing my WPF user controls by using the default ImportAttribute constructor, which in fact will make a shared instance of the class if the creation policy is not specified during export. And as many of my controls were implementing the same interface and I was binding them in my views, I was actually trying to add this shared user control instance to different visual elements, which is not permited by WPF (and so the exception).
I marked my imports using the RequiredCreationPolicy set to NonShared and everything got back in order! That was all about learning MEF...

Resources