Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

-I have tried and tried to figure out what is going on with this assignment and

ID: 3534552 • Letter: #

Question

-I have tried and tried to figure out what is going on with this assignment and its not in my textbook anywhere and my teacher is no help. The picture is a sample of what it is suppose to look like. I really want to grasp this concept, but time is a factor.

Please help!

------------------------------------------------------------------------------------------------------------------


Write a WPF application that can dynamically load resource dictionaries at runtime. Styles in WPF can be grouped together into resource dictionaries, your application should allow users to select or type in XAML file name that contains a resource dictionary. The styles within this resource dictionary are then loaded by your application and applied to a Grid control.

Hints to load a resource:
To load a XAML file from disk, you will first need to open that file as a file stream. Next you can use the XamlReader class from the System.Windows.Markup namespace to load that file stream and cast it into a ResourceDictionary. You can then clear any old dictionaries that were previously loaded. Next you add to the MergedDictionaries collection of the current Window

Explanation / Answer

<ResourceDictionary
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Grid"
x:Key="DynamicGridBrush">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1">
<GradientStop Offset="0"
Color="LightBlue" />
<GradientStop Offset="0.65"
Color="LightGreen" />
<GradientStop Offset="1"
Color="White" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

Next, is the Blue.xaml resource dictionary.

<ResourceDictionary
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Grid"
x:Key="DynamicGridBrush">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1">
<GradientStop Offset="0"
Color="Blue" />
<GradientStop Offset="0.65"
Color="LightBlue" />
<GradientStop Offset="1"
Color="AliceBlue" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

private void DynamicLoadStyles()
{
string fileName;

fileName = Environment.CurrentDirectory() +
@"Dictionaries" + txtXamlName.Text;

if (File.Exists(fileName))
{
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
// Read in ResourceDictionary File
ResourceDictionary dic =
(ResourceDictionary)XamlReader.Load(fs);
// Clear any previous dictionaries loaded
Resources.MergedDictionaries.Clear();
// Add in newly loaded Resource Dictionary
Resources.MergedDictionaries.Add(dic);
}
}
else
MessageBox.Show("File: " + txtXamlName.Text +
" does not exist. Please re-enter the name.");
}

VB.NET
Private Sub DynamicLoadStyles()
Dim fileName As String
Dim dic As ResourceDictionary = Nothing

fileName = Environment.CurrentDirectory & _
"Dictionaries" & txtXamlName.Text

If File.Exists(fileName) Then
Using fs As FileStream = New FileStream(fileName, _
FileMode.Open)
' Read in ResourceDictionary File
dic = CType(XamlReader.Load(fs), ResourceDictionary)
' Clear any previous dictionaries loaded
Resources.MergedDictionaries.Clear()
' Add in newly loaded Resource Dictionary
Resources.MergedDictionaries.Add(dic)
End Using
Else
MessageBox.Show("File: " + fileName + _
" does not exist. Please re-enter.")
End If
End Sub