How to reuse usercontrols accrossdifferent WPF projects - c#

I have a WPF project say Application 1 and inside of it a user control is defined ..I want to resue this user control in another WPF project ,How I will be able to do it??
Both projects are binded into same source control

I am not an expert but let me try. Ensure that your user control is accessible from another project, i.e. there is a reference to the library where this control is defined. Then, at the place where you want this control to be reused, you need to define the namespace and use your control as any other controls. e.g.
<Window x:Class="Test.Window1" Title="Window1" Height="300" Width="600"
...
xmlns:userControls="clr-namespace:MySolution.MyPresentation.MyControls;assembly=MySolution.MyPresentation"
/*controls namespace and assembly*/
>
<Grid>
<userControls:ReusableControl
/*particular properties of ReusableControl */
/>
</Grid>
<window>

best way is:
define your user control in other project only contains your user control as Class library project (Like Scott Say)
but stil you can do this manually with these steps:
copy your user control from App1-pro(from solution explorer)
to App2-pro(even you can don this
from vs to another vs)
open Code
behind file and go to Ctor and find
InitializeComponent-mthod push
f12(or use goto definition from
right-click menu)
change the name
space from application1 to
Application2 namespace(oops save
it!)
change cod-behind-file
namespace like above step(also save
it)
go to xaml file and change
this
x:Class=WpfApplication1.UserControl to x:Class=WpfApplication2.UserControl
Now enjoy your User-control!

Refactor your control into a seperate windows library (dll), and then add that dll to the references for each project.

Related

.NET 6 - WPF CustomControl Template not applied though being in App Resources

We are switching a huge WPF Appl. to .NET 6.0. At least one CustomControl which worked on 4.8 does not get it's template applied which is referenced via Generic.xaml. I am not sure if Generic.xaml is not loaded or something else needs to be considered.
The Style can be added manually to Application.Resources.MergedDictionaries via a simple "Add" call with the Source set to it. I can see the CustomControl Style afterwards with the Key being the correct Type. It is still not applied, as there is no visual representation and no call is made to the overriden OnApplyTemplate method.
If all else fails, can I apply a template manually if I have the given style, like just apply the Style manually to a newly created instance?
Also: the Projects are now SDK-Style, AssemblyInfo.cs was taken over and "generate assembly info" is set to false. It contains the standard ThemeInfo entry.
Just for clarification following the code which successfully finds the Generic.xaml. But before 6.0 Generic.xaml was loaded without doing anything.
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Contracts;component/Themes/Generic.xaml", UriKind.Relative)
});
Thank you all for your help!
I had the same Problem.
maybe u need to add the Assembly.cs at TopLevel of ur CustomControlLibrary
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

Reusable SplitView pane?

I have created a simple SplitView and I'm wondering if I can reuse the code in my <SplitView.Pane>? It's pretty simple, just a few buttons, but I don't want to have to include the same code over and over again on my different pages.
Or is there a way to template the whole SplitView and just modify the <SplitView.Content>on every page? I'd prefer to be able to be able to have the button handlers be global too so that I don't have the same 3 buttons coded in each page .cs file.
What would be the best way to do this?
EDIT: This is for a UWP Windows 10 app.
You can use your own user control to reuse the content of splitview.pane.
UserControl class
Here is an example, my code.. 'FutaLocation' is my usercontrol.
<SplitView x:Name="Splitter" Grid.Row="1"
PanePlacement="Left"
DisplayMode="Inline"
OpenPaneLength="500"
PaneClosed="Splitter_PaneClosed"
PaneClosing="Splitter_PaneClosing"
>
<SplitView.Pane>
<futaviewcontrols:FutaLocation x:Name="futaLocation"/>
</SplitView.Pane>
<Pivot x:Name="directPivot"
If you can make a server control out of it, you can resuse and configure however you like. If this is not possible, you can make a code snippet and then place on page and alter as needed.

How can I separate `<style>` from `<Window.Resources>`?

I have a lot of <Style> in <Window.Resources>.
How can I separate <Style> from <Window.Resources>?
Do I have to make a new Window file and use <Application.Resources>?
I also have a lot of those.
in my <Window.Resources>. Is it also possible to separate those?
You add those styles in ResourceDictionary and use it where ever required. You can add application level or Window Level.
http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/03/creating-and-consuming-resource-dictionaries-in-wpf-and-silverlight.aspx
http://www.codeproject.com/Articles/35346/Using-a-Resource-Dictionary-in-WPF
You can create a ResourceDictionary file. It's just a xaml where you can put styles, control templates etc. See here for more info: http://msdn.microsoft.com/en-gb/library/cc903952(v=vs.95).aspx
You can then attach this dictionary to your project in app.xaml.

The name 'foo' is not exist in namespace 'http://foo...'

I have an WPF solution and this solution consist of 3 project:
1-A project that has several WPF user control inside
2-Another project that has several WPF user control inside
3-A project which has Resources for 2 WPF projects above.
As you know, if you have common settings for you views like that
-Using Same FontFamily.
-Using same FontSize
-Using same FontWeight
-Using same BackroundBrush for all your User Controls etc.. You need to declare this setters in you all usercontrol tags like below:
<UserControl ....
FontFamily="{DynamicResource MyFontFamily}"
FontSize="{DynamicResource MyFontSize}"
FontWeight="{DynamicResource MyFontWeight}"
Background="{DynamicResource MyAppBgBrush2}"
Width="250" d:DesignHeight="350">
<Grid/>......
But I dont want to write same setters in all my UserControls. For thi reason, I decided to move this property setting in to a new c# file and locate it in Resource Project.
using System.Windows.Controls;
namespace Resources
{
public class PageBase : UserControl
{
public PageBase()
{
SetResourceReference(FontFamilyProperty, "MyFontFamily");
SetResourceReference(FontSizeProperty, "MyFontSize");
SetResourceReference(FontWeightProperty, "MyFontWeight");
SetResourceReference(BackgroundProperty, "MyAppBgBrush2");
}
}
}
So, In my Resource project, I adited AssemlyInfo.cs file like this:
[assembly: System.Windows.Markup.XmlnsDefinition("http://schemas.sat.com/winfx/2010/xaml/internalresources", "Resources")]
This edit gives me ability to declare/create a user control like below:
<internalresources:PageBase
xmlns:internalresources="http://schemas.sat.com/winfx/2010/xaml/internalresources">
<Grid>DoWhatEver<Grid/>
<internalresources:PageBase/>
From now, I do not have to create a usercontrol view which its tags start with
<UserControl...., I can start with <internalresources:PageBase......
My Question is that, VisualStudio 2010 can show me Design of all my user control bu Expression blend can not. Interesting part is that both in VS and Blend, my project compiling without any error But when I try to open my views in blend it says:
-The namespace 'PageBase' does not exist in namespace "http://schemas.sat.com/winfx/2010/xaml/internalresources"
P.S: References are added properly to my Project and My project was suitable to open with blend.

asp.net Cannot create UserControl in class

My ultimate goal is to create a UserControl in a class in my App_Code folder and render the html from that into a string.
The rending of HTML can be done using this example I believe:
How do I get the HTML output of a UserControl in .NET (C#)?
My Question:
I created my user control
public partial class Controls_MyUserControl : System.Web.UI.UserControl
I've registered it in my web.config
<add tagPrefix="UC" src="~/Controls/MyUserControl.ascx" tagName="MyUserControl" />
I can reference this on a page just fine
<UC:MyUserControl ID="MyUserControl1" runat="server" />
Yet when I try to create an object of this control type in a class (in my app_code folder) it doesn't allow me.
Controls_MyUserControl dummy = new Controls_MyUserControl();
The potentially strange thing is when I put this code into a code behind of a page, it works.
Any ideas on what I need to do to have the usercontrol to be able to be created in my class in the app_code folder?
My guess is, is that I need to reference the control in a "using" statement at the top, but I'm not sure what that'd be.
It's not in a namespace to my knowledge (at least there's no namespace, in the code behind of the actually user controls). Though I'm not sure if it inherits one from the System.Web.UI.UserControl.
Thanks in advance
Some workaround as posted by Scott Alen http://www.velocityreviews.com/forums/t119801-accessing-web-user-control-from-class-in-app_code-folder.html
Suppose you have all your user controls in ~/UserControls/
Then on your code behind add the following;
using YourSpaceName.UserControls;
This will set reference to any user control in that location.
Note: if you are using a user control within another, make sure to create a folder for each of them. I have experiencing problems, specifically with VB where it would compile but give me a runtime error. Since I encounter that problem I create my user controls each in it's own folder.

Categories