I've created a simple testing class library using silverlight 4, and I want to use it in my WP7 project.
I referenced the dll in the WP7 project and couldn't use the control I've created. I need to know what statement I should add into my xaml file before using the control.
this is the xaml file of the class library (only a rectangle):
<UserControl x:Class="NabbeshControls.ProfileBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Rectangle Fill="#FFABABFF" Margin="10" Stroke="Black"/>
</Grid>
</UserControl>
Thanks...
Assuming the namespace for the control in your library is "NabbeshControls" and the dll is called "Nabbesh.dll", add the following to the top of the XAML in which u want to use it:
xmlns:tryNabbesh="clr-namespace:NabbeshControls;assembly=Nabbesh"
Related
I'm developing a UI library project. It contains set of pages that will be used in two different UWP app projects. The problem is that in one project, the developers uses a theme that modifies the appearance of the basic controls such as the font size of a textblock or TextBox, the margin of the ContentControls etc. In my library, I used pretty basic controls text box, combobox, date picker. It affects how the pages in the UI library that I'm working on looks. I want the pages in my library to look like a basic input form. How can I tell the UI library to not follow whatever theme the Application is using?
How can I tell the UI library to not follow whatever theme the Application is using?
You could add a 'Resource Dictionary' file in your UI library project. In the 'dictionary.xaml' file, you could define a basic style for the controls which is used in your UI library pages. Then, the controls will not be affected by the resource style in the main project.
For example, I add a 'generic.xaml' file in the UI library project and define a style for the button like the following:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ClassLibraryUI">
<Style TargetType="Button" BasedOn="{StaticResource ButtonRevealStyle}">
</Style>
</ResourceDictionary>
I have a 'BlankPage1.xaml' in the UI library.
<Page
x:Class="ClassLibraryUI.BlankPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ClassLibraryUI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="generic.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
<Grid>
<Button Content="Hello UWP"></Button>
</Grid>
</Page>
In the main project, I defined a style for the button in 'App.xaml':
<Application
x:Class="AppUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppUI">
<Application.Resources>
<Style TargetType="Button">
<Setter Property="Foreground" Value="Red"></Setter>
<Setter Property="Background" Value="LightBlue"></Setter>
<Setter Property="Width" Value="200"></Setter>
<Setter Property="Height" Value="50"></Setter>
</Style>
</Application.Resources>
</Application>
On the 'MainPage.xaml':
<StackPanel>
<Button Content="Navigate" Click="Button_Click"></Button>
<Frame x:Name="frame"></Frame>
</StackPanel>
In the 'MainPage.xaml.cs', I use Frame control to navigate to the 'BalnkPage1' in the UI library.
You would see the 'Navigate' button on the MainPage will use the style in 'Application.Resources', but the button on 'BlankPage1' will use the style in its own 'generic.xaml' file.
Status
I have the following (Visual Studio 2017) solution configuration:
created a new solution with one .csproject for post-build commands
added several other .csprojects to the solution via add existing project
now I want to add a plain .xaml file, which works via add an existing item to the solution, but throws the error:
Object reference not set to an instance of an object.
Problem
The item still appears, but a double-click to open it throws the same error and does not let me edit it.
Unwanted fixes
I can prevent this error by two ways (which I do not want):
rename the .xaml files to e.g. a .xaml.txt file ending (and name them back within the post-solution-build commands)
put the .xaml files into an own .csproj folder with a .cs file only defining the empty namespace ThisIsMyGraphicsElement { } and a Properties\AssemblyInfo.cs file
So my question is
How can I add and edit a plain .xaml file to my Visual Studio solution?
Edit
Here is an example code:
```
<UserControl
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="410" d:DesignWidth="800">
<Grid Background="#FFe5e5e6">
<Grid.RowDefinitions>
</Grid.RowDefinitions>
<StackPanel HorizontalAlignment="Left" Height="200" VerticalAlignment="Top" Width="500" Margin="150,140,0,0" Grid.RowSpan="2">
<TextBox x:Name="textBox1" HorizontalAlignment="Center" Height="100" TextWrapping="Wrap" VerticalAlignment="Top" Width="366.865" Margin="0" Background="{x:Null}" FontSize="24" Text="Live happily ever after...
" BorderBrush="{x:Null}"/>
</StackPanel>
</Grid>
</UserControl>
```
As mentioned in comments/answers, adding a new (empty) .xaml file and pasting the code did not work as well.
Not sure what is happening here. You could try to create a new XAML within your project and then paste the XAML from the file you want to include. That's what I would try.
Edit: Not being able to comment.... I'm not the first to suggest this.
I have the following simplified scenario:
A Windows 10 Class Library containing a UserControl with an Image Control that displays an image from a file inside the library:
<Image Source="Assets/myimage.png"></Image>
Now I have different App-Projects (UWP) containing this library and displaying my Image Control.
In these Projects are different images that should replace the original myimage.png. Simply putting the new image in Assets Folder of my App-Project does not work (thats the way it works in Android).
Summary: I want to replace a Image-File in a Library with a file in my actual App-Project.
Edit:
The Control inside the Library:
<UserControl
x:Class="mLib.TestImageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Image x:Name="Image" Source="Assets/myimage.png"></Image>
</Grid>
Usage Inside the App:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<mlibrary:TestImageView/>
</Grid>
Put slash in the beginning, like this:
<Image x:Name="Image" Source="/Assets/myimage.png"></Image>
Replacing will work fine then. But I don't think you'll be able to use original image in referencing projects after that.
In my Windows Phone 7.5 application i want to have a merged ResourceDictionary filled with DataTemplates. To achieve this, i created a file called "DataTemplates.xaml" and filled it with DataTemplates like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="ItemTemplate">
<!-- xaml -->
</DataTemplate>
<DataTemplate x:Key="GroupHeaderTemplate">
<!-- xaml -->
</DataTemplate>
<DataTemplate x:Key="GroupItemTemplate" >
<!-- xaml -->
</DataTemplate>
</ResourceDictionary>
In page where i want to use that DataTemplates i wrote this code:
<phone:PhoneApplicationPage.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/myAssemblyName;component/Resources/DataTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</phone:PhoneApplicationPage.Resources>
But, when i debug and try to navigate to the page that contains the ResourceDictionary i got a XamlParseException: Failed to assign to property 'System.Windows.ResourceDictionary.Source'
How can i do? :( Thank you!
Ok, i spent about 20 minutes to write question and after rereading it i found solution, so i answer myself: i accidentally wrote the namespace of solution instead of its assembly name! That's all, now the ResourceDictionary loads properly :)
I did a custom button control in WPF and I am trying to implement it in WinForms (c#). Now, the control is working and it compiles well, but I can't seem to access the elementhost - the control was not shown in the toolbox, and afterwards I saw it's frozen, i.e. I can't add it to the form. Since the project build process went without errors or warnings, I don't know what is causing the problem. Any help would be appreciated, thanks.
EDIT: Here's the code of the xaml file:
<Button x:Class="proba4.UserControl1"
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="120" d:DesignWidth="300" IsEnabled="True">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Image Name="Normal" Source="C:\stuff\off_button.gif"/>
<Image Name="Pressed" Source="C:\stuff\on_button.gif" Visibility="Hidden"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
May be it is because you inherit your control from non-usercontrol class? In WPF the first rule is avoiding control inheritance. Try to use composition