Open/Load view from another project of same solution in WPF - wpf

In my application, in a single solution, there are two different project.
Lets say, Project A and Project B.
From the view of first project A, on button click event, I want to load the view from the second project B.
Could you please guide me.
Thanks.
Ruhul

Add a reference to Project B from Project A in Visual Studio: Project->Add Reference->Projects->Solution.
Then you can create an instance of the view class that is defined in Project B and use it as appropriate:
private void btnClick_In_Project_A(object sender, RoutedEventArgs e)
{
//if viewB is a window:
ProjectB.ViewB viewB = new ProjectB.ViewB();
viewB.Show();
//or if viewB is a UserControl:
Window win = new Window();
win.Content = new ProjectB.ViewB();
win.Show();
}
Project B should be a WPF User Control Library or a WPF User Control Library.

Related

WPF published application crashing when changing theme

The application is working fine when i run it from Visual Studio but if i run my application when its published it crashes. This is the first application i have published so i dont know where to start.
I have 2 combo boxes that let you choose the theme color of the application as soon as i choose one it crashes.
This is using Mahapps Metro
The combobox's item source is a metro thememanager
ItemsSource="{x:Static metro:ThemeManager.Accents}"
This is the Selection Changed code
private void ColorsSelectorOnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedColor = this.ColorsSelector.SelectedItem as KeyValuePair<string, Color>?;
if (selectedColor.HasValue)
{
var theme = ThemeManager.DetectAppStyle(Application.Current);
ThemeManagerHelper.CreateAppStyleBy(selectedColor.Value.Value, true);
Application.Current.MainWindow.Activate();
}
When creating a runtime accent resource dictionary:

WinForm progressbar not working when I open that form in Wpf application?

I created a Windows form project in Visual studio and used a progress bar with style as marquee and I created another wpf project and added windows project reference to the newly created wpf project and call the form in a button click event as
private void Button_Click(object sender, RoutedEventArgs e)
{
Form1 form = new Form1();
form.Show();
}
But the progress bar inside the form is not working.
I fixed the above issue by adding the following lines
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

Dynamically Change Content by clicking RibbonPane using Prism

I'm using Microsoft Ribbon and Prism to develop my application. I have in my main window 2 regions: one for the ribbon and the other to inject a view depending on the button clicked in my ribbon.
That works pretty good, but I would like to have the same functionality if I click a specific ribbon tab.
Has anyone done anything like this using Prism?
As you wanted, here is the code using a Button. This code is in the VM of the Ribbon...when the button is clicked the event goes to OnShowConfiguration. This method load a new View in my GeneralContentRegion and also a new RibbonTab.
private void OnShowConfiguration()
{
loadView(PrismViewsNames.GeneralContentMainView, PrismRegionsNames.ContentRegion);
loadView(PrismViewsNames.GeneralRibbonTab, PrismRegionsNames.RibbonMenuRegion);
}
private void loadView(string viewToShow, string regionWhereToShow)
{
var regionManager = (RegionManager)ServiceLocator.Current.GetInstance<IRegionManager>();
var uri = new Uri(viewToShow, UriKind.Relative);
regionManager.RequestNavigate(regionWhereToShow, uri);
}
Myabe that helps you, Ayyappan Subramanian ;)

opening a prism module on new window on a registered region

I have an application with various modules.
I have divided my main shell (xaml) into different regions and now I can load modules on those regions.
But I have requirement where in on click of some button I have to open a new window and then a new module will load on the new window.
I created a new window and I am opening that window , but the window is having a region which the RegionManager of main application does not recognize.
How do I load a module on a region which is not on main window but on child window ?
You can find a quick sample solution for your problem in the following SkyDrive public folder as "RegionInChildWindowWithNavigation":
RegionInChildWindowWithNavigation
Based on my understanding, the problem you mentioned would be related on setting the RegionManager property on the ChildWindow View that cause the defined ModalWindowRegion be reachable from the RegionManager. Below is the ModalDialog ChildWindow view constructor from the aforemention sample. Notice that it also adds an event handler to properly remove all the views in the ChildWindow when closed.
[ImportingConstructor]
public ModalDialog(IRegionManager rm)
{
this.rm = rm;
this.SetValue(RegionManager.RegionManagerProperty, rm);
InitializeComponent();
this.Closed += new EventHandler(WindowsView_Closed);
}
void WindowsView_Closed(object sender, EventArgs e)
{
while (rm.Regions["ModalWindowRegion"].Views.Count() > 0)
{
rm.Regions["ModalWindowRegion"].Remove(rm.Regions["ModalWindowRegion"].Views.FirstOrDefault());
}
}
Then, you would just need to RequestNavigate() to the specified Region which is defined in the ChildWindow view from the RegionManager as follows:
ModalDialogWindow.Show();
rm.RequestNavigate("ModalWindowRegion", new Uri("HelloWorldView", UriKind.Relative));
In addition, you may find useful the following CodePlex threads:
Define Region(s) in Childwindows using Prism + MVVM
Display Child window WPF
I hope this helps.

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