I have an Image that I need to load from an external file into a Button.
The Image resides in a folder in the same directory as the final executable file.
/Resources/image.png
It's not included in the Project Resources and it should not be for the needs of the Application.
The Image is loaded and displayed into a UserControl fine, but when I place my UserControl into my main View,
I get the following error:
Cannot locate resource 'views/usercontrols/resources/playerbuttonsicons/repeat-icon.png'.
The UserControl is located under Views/UserControls in my Project Tree as the error says.
I have tried various ways of specifying the image path (absolute, relative, uri, pack etc), but none of them worked.
The problem is to be tackled using xaml only, if possible.
UserControl code:
<UserControl x:Class="MusicPlayer.Views.UserControls.NowPlayingControl"
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"
xmlns:local="clr-namespace:MusicPlayer.Views.UserControls"
mc:Ignorable="d"
d:DesignWidth="400"
x:Name="ParentControl">
<!--Resources-->
<UserControl.Resources>
</UserControl.Resources>
<!--Design-->
<DockPanel x:Name="ParentContainer">
<Button x:Name="btnPlay">
<Button.Background>
<ImageBrush ImageSource="Resources/PlayerButtonsIcons/play-icon.png" />
</Button.Background>
</Button>
</DockPanel>
</UserControl>
MainView code:
<Window x:Class="MusicPlayer.MainView"
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"
xmlns:local="clr-namespace:MusicPlayer"
mc:Ignorable="d"
xmlns:vm="clr-namespace:MusicPlayer.ViewModels"
xmlns:uc="clr-namespace:MusicPlayer.Views.UserControls"
x:Name="ParentControl">
<!--Resources-->
<Window.Resources>
<vm:MainViewModel x:Key="VM" />
</Window.Resources>
<!--Design-->
<Grid x:Name="ParentContainer"
DataContext="{StaticResource VM}">
<uc:NowPlayingControl />
</Grid>
</Window>
I resolved my problem with the following steps.
Included all the external files into my Project Resources. Marked them as Content and Copy Always.
Instead of loading the Images into my UserControl at run-time, I loaded them in my App.xaml Resources.
The Images are now used as StaticResource in my UserControl.
App.xaml
<Application x:Class="MusicPlayer.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MusicPlayer"
xmlns:ex="clr-namespace:SharpUtilities.WPF"
StartupUri="Views/MainView.xaml">
<Application.Resources>
<BitmapImage x:Key="PlayerPlayIcon"
UriSource="Resources/play-icon.png" />
</Application.Resources>
</Application>
UserControl.xaml
<UserControl x:Class="MusicPlayer.Views.UserControls.NowPlayingControl"
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"
xmlns:local="clr-namespace:MusicPlayer.Views.UserControls"
mc:Ignorable="d"
d:DesignWidth="400"
x:Name="ParentControl">
<DockPanel x:Name="ParentContainer">
<Button x:Name="btnPlay">
<Button.Background>
<ImageBrush ImageSource="{StaticResource PlayerPlayIcon}" />
</Button.Background>
</Button>
</DockPanel>
</UserControl>
Related
I'm working on a WPF application using C#. So far, I have added a custom font using a ResourceDictionary on my application. When I run the app I can see the font works fine, but on the Design tab of my XAML it always shows the default font. Could I be missing something to set my designer correctly?
App.xaml
<Application x:Class="Bali.Chat.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Bali.Chat"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Fonts.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Fonts.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<FontFamily x:Key="LatoRegular">pack://application;,,,/Fonts/#Lato Regular</FontFamily>
<FontFamily x:Key="LatoThin">pack://application;,,,/Fonts/#Lato Thin</FontFamily>
</ResourceDictionary>
MainWindow.xaml
<Window x:Class="Bali.Chat.MainWindow"
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"
xmlns:local="clr-namespace:Bali.Chat"
mc:Ignorable="d"
Icon="Images/Logo/logo-small.png"
WindowStyle="None"
AllowsTransparency="True"
Title="Bali Chat"
Height="250" Width="400">
<StackPanel>
<TextBlock Text="Default Font!" FontSize="40" />
<TextBlock Text="Thin Font" FontSize="40" FontFamily="{StaticResource LatoThin}" />
<TextBlock Text="Regular Font" FontSize="40" FontFamily="{StaticResource LatoRegular}" />
</StackPanel>
</Window>
This is what I get on my designer
And this is what I get if I run the application
I really would like to preview what I'm trying to render without having to start the application or check it on the live preview. The tutorial I am following does show the presenter seeing the proper font in the designer, so I know there are some issues. Thanks in advance.
I have created a UserControl with some rounded edged Border as first real element. The actuall Background is transparent.
<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"
xmlns:local="clr-namespace:QP_WPF" x:Class="GUI_WPF_Interior"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:themes="clr-namespace:Xceed.Wpf.Toolkit.Themes;assembly=Xceed.Wpf.Toolkit"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
d:DesignWidth="600
" Background="transparent">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ColorsAndBrushes.xaml" />
<ResourceDictionary Source="ControlTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</UserControl.Resources>
<Border Margin="10" Background="{StaticResource BG_GradientBrush_2}" CornerRadius="12,12,12,12">
....
(the Margin is only to provide a better visual for the problem)
Now I want to display this UserControl in a window. But the area that is used by the margin and the rounded edges stays white.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:qp="clr-namespace:QP_WPF;assembly=QP_WPF"
Title="MainWindow" Height="680" Width="600"
WindowStyle="None"
AllowsTransparency="True"
MouseLeftButtonDown="Window_MouseLeftButtonDown">
<Grid Background="Transparent">
<qp:GUI_WPF_Interior x:Name="GUIInterior" Background="Transparent"/>
</Grid>
</Window>
What do I need to doe so that the Window only displays my UserControls parts that are not transparent?
Try to add also background=transparent to the window besides AllowTransparency
I'm trying to do the Getting Started instructions here: http://mahapps.com/MahApps.Metro/guides/quick-start.html.
I've gotten the latest pre-release (tried with stable too), I'm not getting the same window the guide is producing. I'm getting a transparent window and titlebar, so it looks like a floating titlebar, and minimize, maximize and close buttons.
When I add the styling I get a white background with a blue titlebar, but no shadow. Am I doing something wrong here or has anyone else experienced this?
Thanks.
EDIT: here's the XAML
Main Window
<Controls:MetroWindow x:Class="Metro.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Title="MainWindow" Height="900" Width="1600">
</Controls:MetroWindow>
App.xaml
<Application x:Class="Metro.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
As I mentioned, I followed the getting started instructions, I copy and pasted the exact same code, and got a different result.
EDIT
The quick start guide and the MetroWindow help now updated (04.09.2014).
The screenhots/examples at the quickstart are not quite updated.
You can have a border
<controls:MetroWindow x:Class="MahApps.Metro.Simple.Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
Title="MainWindow"
Height="200"
Width="600"
BorderBrush="{DynamicResource AccentColorBrush}"
BorderThickness="1"
WindowStartupLocation="CenterScreen">
</controls:MetroWindow>
or a glow border
<controls:MetroWindow x:Class="MahApps.Metro.Simple.Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
Title="MainWindow"
Height="200"
Width="600"
GlowBrush="{DynamicResource AccentColorBrush}"
WindowStartupLocation="CenterScreen">
</controls:MetroWindow>
or a drop shadow
<controls:MetroWindow x:Class="MahApps.Metro.Simple.Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:behaviours="http://metro.mahapps.com/winfx/xaml/shared"
Title="MainWindow"
Height="200"
Width="600"
ResizeMode="CanResizeWithGrip"
WindowTransitionsEnabled="False"
WindowStartupLocation="CenterScreen">
<i:Interaction.Behaviors>
<behaviours:BorderlessWindowBehavior AllowsTransparency="False"
EnableDWMDropShadow="True" />
<behaviours:WindowsSettingBehaviour />
<behaviours:GlowWindowBehavior />
</i:Interaction.Behaviors>
</controls:MetroWindow>
Update
EnableDWMDropShadow has been moved to MetroWindow in version 0.13 alpha (latest version)
<controls:MetroWindow x:Class="MahApps.Metro.Simple.Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:behaviours="http://metro.mahapps.com/winfx/xaml/shared"
Title="MainWindow"
Height="200"
Width="600"
EnableDWMDropShadow="True"
ResizeMode="CanResizeWithGrip"
WindowTransitionsEnabled="False"
WindowStartupLocation="CenterScreen">
</controls:MetroWindow>
hope that helps
i am new to xml in silverlight.i have one small xml file below
<FlowActivities>
<SequenceFlow >
<FlowWriteLine>
hiiii
</FlowWriteLine>
</SequenceFlow>
</FlowActivities>
in this i want to hardcode some namespace in rootnode.like
<FlowActivities x:Class="WorkflowConsoleApplication1.modify"
xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
mc:Ignorable="sap2010"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
sap2010:ExpressionActivityEditor.ExpressionActivityEditor="C#"
xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SequenceFlow >
<FlowWriteLine>
hiiii
</FlowWriteLine>
</SequenceFlow>
</FlowActivities>
for getting this wht i have to do..? pls sort this out..?
XAML is not a current XML file, is a language based on XML. Therefore you can not write random, non-existent XML tags.
To hardcode a string in SL XAML file:
<UserControl
x:Class="Test_SL_HardcodeString.MainPage"
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"
xmlns:system="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<system:String x:Key="myString">This is a test string</system:String>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<TextBox Text="{StaticResource myString}"/>
</Grid>
</UserControl>
You can't. You have to set the variable like JoanComasFdz said.
If you must use the same format, you can create a separate class(viewmodel) for eg. MyXMLData.cs to read and parse xml file. Read the XML node and set the class variable "theString" from this class. In XAML, you can create an instance of the class in the resources section and set the data context of the Grid or the textbox to that object.
<UserControl
x:Class="Test_SL_HardcodeString.MainPage"
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"
xmlns:system="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d"
xmlns:viewmodel="clr-namespace:MyNameSpace.ViewModels"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<viewmodel:MyXMLData x:key="myxmldataclass"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource myxmldataclass}" >
<TextBox Text="{StaticResource theString}"/>
</Grid>
</UserControl>
I want to do something like this:
<Window x:Class="WpfApplication10.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Button x:Key="butt" Content="Yeah!" />
</Window.Resources>
<Grid>
<!-- Place button here -->
</Grid>
</Window>
How to do it? I want to create library of small user controls in single file.
<Grid>
<StaticResource ResourceKey="butt"/>
</Grid>
Should work...