XAML code won't compile - c#

Please can someone be of help. Below is my XAML code that won't compile
<Window x:Class="XAMLCalendar.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:XAMLCalendar"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Orientation = "Horizontal">
<!-- Create a Calendar that displays dates through
Januarary 31, 2015 and has dates that are not selectable. -->
<Calendar Margin = "20" SelectionMode = "MultipleRange"
IsTodayHighlighted = "false"
DisplayDate = "1/1/2015"
DisplayDateEnd = "1/31/2015"
SelectedDatesChanged = "Calendar_SelectedDatesChanged"
xmlns:sys = "clr-namespace:System;assembly =mscorlib">
<Calendar.BlackoutDates>
<CalendarDateRange Start = "1/2/2015" End = "1/4/2015"/>
<CalendarDateRange Start = "1/9/2015" End = "1/9/2015"/>
<CalendarDateRange Start = "1/16/2015" End = "1/16/2015"/>
<CalendarDateRange Start = "1/23/2015" End = "1/25/2015"/>
<CalendarDateRange Start = "1/30/2015" End = "1/30/2015"/>
</Calendar.BlackoutDates>
<Calendar.SelectedDates>
<sys:DateTime>1/5/2015</sys:DateTime>
<sys:DateTime>1/12/2015</sys:DateTime>
<sys:DateTime>1/14/2015</sys:DateTime>
<sys:DateTime>1/13/2015</sys:DateTime>
<sys:DateTime>1/15/2015</sys:DateTime>
<sys:DateTime>1/27/2015</sys:DateTime>
<sys:DateTime>4/2/2015</sys:DateTime>
</Calendar.SelectedDates>
</Calendar>
</StackPanel>
</Grid>
</Window>
Below is the c# partial class behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace XAMLCalendar
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
{
var calendar = sender as Calendar;
// ... See if a date is selected.
if (calendar.SelectedDate.HasValue)
{
// ... Display SelectedDate in Title.
DateTime date = calendar.SelectedDate.Value;
this.Title = date.ToShortDateString();
}
}
}
}
Below is the error messages I get
Severity Code Description Project File Line Suppression State
Error  Unknown build error, ''clr-namespace:System;assembly =mscorlib' mapping URI is not valid. Line 6 Position 9.' XAMLCalendar C:\Users\mypc\documents\visual studio 2017\Projects\XAMLCalendar\XAMLCalendar\MainWindow.xaml line 6 
Severity Code Description Project File Line Suppression State
Error  The URI "clr-namespace:System;assembly =mscorlib" is not a valid namespace identifier. XAMLCalendar C:\Users\mypc\documents\visual studio 2017\Projects\XAMLCalendar\XAMLCalendar\MainWindow.xaml line 20 
Severity Code Description Project File Line Suppression State
Error  The name "DateTime" does not exist in the namespace "clr-namespace:System;assembly =mscorlib". XAMLCalendar C:\Users\mypc\documents\visual studio 2017\Projects\XAMLCalendar\XAMLCalendar\MainWindow.xaml  line 31 
Severity Code Description Project File Line Suppression State
Error  The name "DateTime" does not exist in the namespace "clr-namespace:System;assembly =mscorlib". XAMLCalendar C:\Users\mypc\documents\visual studio 2017\Projects\XAMLCalendar\XAMLCalendar\MainWindow.xaml  line 32 
........and the same errors as the one directly above, up to line 37
Please can someone tell me why it won't compile. I've tried adding mscorlib reference to my project, but it won't let me. Saying "This component is already automatically referenced by the build system" 
Also there's a wriggle line under xmlns:sys  in the Calender element in the XAML above. When I hover my mouse over it, the intellisense says "The URI "clr-namespace:System;assembly =mscorlib" is not a valid namespace identifier
Cheers. Am using Microsoft visual studio 2017 community

Just remove the extra whitespace in namespace declaration.
<Window x:Class="XAMLCalendar.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:XAMLCalendar"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Orientation = "Horizontal">
<!-- Create a Calendar that displays dates through
Januarary 31, 2015 and has dates that are not selectable. -->
<Calendar Margin = "20" SelectionMode = "MultipleRange"
IsTodayHighlighted = "false"
DisplayDate = "1/1/2015"
DisplayDateEnd = "1/31/2015"
SelectedDatesChanged = "Calendar_SelectedDatesChanged"
xmlns:sys = "clr-namespace:System;assembly=mscorlib">
<Calendar.BlackoutDates>
<CalendarDateRange Start = "1/2/2015" End = "1/4/2015"/>
<CalendarDateRange Start = "1/9/2015" End = "1/9/2015"/>
<CalendarDateRange Start = "1/16/2015" End = "1/16/2015"/>
<CalendarDateRange Start = "1/23/2015" End = "1/25/2015"/>
<CalendarDateRange Start = "1/30/2015" End = "1/30/2015"/>
</Calendar.BlackoutDates>
<Calendar.SelectedDates>
<sys:DateTime>1/5/2015</sys:DateTime>
<sys:DateTime>1/12/2015</sys:DateTime>
<sys:DateTime>1/14/2015</sys:DateTime>
<sys:DateTime>1/13/2015</sys:DateTime>
<sys:DateTime>1/15/2015</sys:DateTime>
<sys:DateTime>1/27/2015</sys:DateTime>
<sys:DateTime>4/2/2015</sys:DateTime>
</Calendar.SelectedDates>
</Calendar>
</StackPanel>
</Grid>
</Window>

Related

Need help to refresh a brush of a visual child object (a Border) within viewport2DVisual3D object class

I am using the Planerator control which works great. The only problem is that the brush of the Border object I added inside it doesn't get changed. It rotates well, but the brush seems frozen. I tried many things, but no can do.
Control link: https://github.com/pauldotknopf/WPF-MediaKit/blob/master/SampleApplication/Controls/Planerator.cs
I search a lot on many help sites, including stackoverflow. Here is what I tried without any success;
Important: I am working from code behind, not XAML.It worked fine in XAML
this.UpdateLayout();
this.InvalidateMeasure();
this.InvalidateVisual();
this.InvalidateArrange();
((UIElement)_originalChild).UpdateLayout();
var child = VisualTreeHelper.GetChild(viewport2DVisual3D, 0);
((UIElement)child).UpdateLayout();
((UIElement)child).InvalidateMeasure();
((UIElement)child).InvalidateVisual();
((UIElement)child).InvalidateArrange();
From this code;
// Interactive frontside Visual3D
Viewport2DVisual3D frontModel = new Viewport2DVisual3D() { Geometry = simpleQuad, Visual = _logicalChild, Material = frontMaterial, Transform = xfGroup };
Now, after testing, I am suspecting that once the object is added inside Viewport2DVisual3D has a child, color & brushes don't get refreshed anymore, but it will until/before that point. See "Visual = _logicalChild" above.
I have no error message, it rotates well, but the brush and color of my border don't get updated if I change the color.
Here is my working markup;
<Window
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:TestPlaneratorInXAML"
xmlns:Planerator="clr-namespace:Planerator;assembly=Planerator" x:Class="TestPlaneratorInXAML.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Planerator:Planerator x:Name="aPlaneratorControl" Height="100" VerticalAlignment="Top" Width="100">
<Border BorderBrush="Black" BorderThickness="5" HorizontalAlignment="Center" Height="100" VerticalAlignment="Top" Width="100" CornerRadius="5" Background="Black"/>
</Planerator:Planerator>
<Button x:Name="ChangeBackground" Content="Change background" HorizontalAlignment="Left" VerticalAlignment="Top" Width="160" PreviewMouseDown="ChangeBackground_PreviewMouseDown"/>
</Grid>
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace TestPlaneratorInXAML
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ChangeBackground_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
((Border)aPlaneratorControl.Child).Background = Brushes.Red;
}
}
}

How do I append a Run to an existing Paragraph in a FlowDocument?

I need to expand an existing Run to include some new text (with different formatting) without adding an additional paragraph. Is this possible?
When I inspect the properties of the FirstBlock on the Document, I do not see any property which allow me to drill down into the paragraph so that I can add a Run to it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.flowdoc.Document = new FlowDocument();
Run r = new Run("Hello ");
r.Background = new SolidColorBrush(Colors.Yellow);
r.FontSize = 14;
Paragraph p = new Paragraph(r);
flowdoc.Document.Blocks.Add(p);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Run r = new Run("World");
r.Background = new SolidColorBrush(Colors.LightCyan);
//Append run to existing run
//
}
}
}
<Window x:Class="WpfApp3.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:WpfApp3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<FlowDocumentReader Grid.Column="0" x:Name="flowdoc"></FlowDocumentReader>
<Button Grid.Column="1" Content="append" Click="Button_Click"></Button>
</Grid>
</Window>
To get your Paragraph you may iterate the Blocks property of the document.
You can then easily add a new Run to the Block's Inlines collection.
private void Button_Click(object sender, RoutedEventArgs e)
{
Run r = new Run("World");
r.Background = new SolidColorBrush(Colors.LightCyan);
//Append run to existing run
var p = flowdoc.Document.Blocks.OfType<Paragraph>().First();
p.Inlines.Add(r);
}

WPF MediaElement: Video opened twice

I want to use a MediaPlayer for videos and pictures with the MediaElement. I have already done tests, the MediaElement can also display pictures.
Currently I have the problem that the MediaElement seems to open twice.
Here is the example code:
XAML:
<Window x:Class="TestMediaElement.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:TestMediaElement"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="10">
<MediaElement Name="mediaPlayer" MediaOpened="media_MediaOpened" LoadedBehavior="Play" UnloadedBehavior="Manual"/>
</Grid>
</Window>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TestMediaElement
{
public partial class MainWindow : Window
{
int currentMediaIndex = 0;
string[] Documents;
public MainWindow()
{
InitializeComponent();
Documents = System.IO.Directory.GetFiles("C:/Users/Feller/Desktop/Test/");
Uri first = new Uri(Documents[0], UriKind.RelativeOrAbsolute);
mediaPlayer.Source = first;
mediaPlayer.MediaOpened += media_MediaOpened;
}
private void media_MediaOpened(object sender, RoutedEventArgs e)
{
Console.WriteLine("Video opened");
}
}
}
Another problem is that images close automatically after about 5 seconds.
Can anyone help me with these problems?
Thank you very much!
MediaOpened event is subscribed twice. Either remove from the xaml or remove from code.
//mediaPlayer.MediaOpened += media_MediaOpened;

Adding models/content to the HelixViewport3D in Helix 3D Toolkit

I am trying to load and display a 3d model in the HelixViewport3D.
I can get as far as loading the model (OBJ), but I cannot understand how to get the model into the viewport.
Here's a screenshot of my WPF form...
The viewprot is named as 'myView' - I thought I could hook into that to add my model, but I don't see anything obvious to use.
Here's my XAML of the form :
<Window x:Class="HelixTrial.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid HorizontalAlignment="Left" Height="250" Margin="241,37,0,0" VerticalAlignment="Top" Width="250">
<HelixToolkit:HelixViewport3D x:Name="myView" ZoomExtentsWhenLoaded="True">
<!-- Remember to add light to the scene -->
<HelixToolkit:SunLight/>
<!-- You can also add elements here in the xaml -->
<HelixToolkit:GridLinesVisual3D Width="8" Length="8" MinorDistance="1" MajorDistance="1" Thickness="0.01"/>
</HelixToolkit:HelixViewport3D>
</Grid>
</Grid>
And here is the code for my form.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;
namespace HelixTrial
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObjReader CurrentHelixObjReader = new ObjReader();
Model3DGroup MyModel = CurrentHelixObjReader.Read("C:/Users/Roger/Desktop/cube/cube.obj");
// Now how to load it into the viewport... ?
}
}
}
You can see where I am stuck. Could someone help get me on track please.
After some experimenting I found the solution.
I added the following to my XAML :
<ModelVisual3D x:Name="foo"/>
The trick was to give it a name, ie 'foo' for example. The XAML will now look like this :
<Window x:Class="HelixTrial.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid HorizontalAlignment="Left" Height="250" Margin="241,37,0,0" VerticalAlignment="Top" Width="250">
<HelixToolkit:HelixViewport3D x:Name="myView" ZoomExtentsWhenLoaded="True">
<!-- Remember to add light to the scene -->
<HelixToolkit:SunLight/>
<ModelVisual3D x:Name="foo"/>
<!-- You can also add elements here in the xaml -->
<HelixToolkit:GridLinesVisual3D Width="8" Length="8" MinorDistance="1" MajorDistance="1" Thickness="0.01"/>
</HelixToolkit:HelixViewport3D>
</Grid>
</Grid>
Then in the code (as per what I posted in my original question above) you can do this :
ObjReader CurrentHelixObjReader = new ObjReader();
Model3DGroup MyModel = CurrentHelixObjReader.Read("C:/Users/Roger/Desktop/cube/cube.obj");
// Display the model
foo.Content = MyModel;
Easy when you find out how ;)
# This is my code which works fine #
<Window x:Class="Helix_Car_Demo.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:Helix_Car_Demo"
xmlns:helix="http://helix-toolkit.org/wpf"
mc:Ignorable="d"
Background="Black"
WindowState="Maximized" WindowStyle="None"
Title="MainWindow" Height="350" Width="525">
<Grid>
<helix:HelixViewport3D Name="viewport3D" ZoomExtentsWhenLoaded="True" MouseDoubleClick="viewport3D_MouseDoubleClick">
<helix:SunLight/>
</helix:HelixViewport3D>
</Grid>
Here is my .cs File
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ModelVisual3D device = new ModelVisual3D();
device.Content = getModel("hearse.3ds");
viewport3D.Children.Add(device);
}
public Model3D getModel(string path)
{
Model3D device = null;
try
{
viewport3D.RotateGesture = new MouseGesture(MouseAction.LeftClick);
ModelImporter import = new ModelImporter();
device = import.Load(path);
}
catch (Exception e)
{ }
return device;
}
}
}

C# WPF ChartingToolKit: How to get independent-axis value from ColumnSeries in MouseRightButtonDown?

I have a ColumnSeries with data bound to it and it displays correctly. When a column is right-clicked, I want to figure out what the independent-axis (X) value is. Ideally, I want to display a context menu with that information.
I have a MouseRightButtonDown handler, but cannot figure out how to perform the hit test to get the X-axis information.
I have selection enabled, but do not want to have to select a column prior to right-click.
Any help would be appreciated!
You can walk up the visual tree looking for a ColumnDataPoint.
Here's a sample graph:
<Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
<Grid.Resources>
<PointCollection x:Key="sampleData">
<Point X="1" Y="6"/>
<Point X="2" Y="4"/>
<Point X="3" Y="8"/>
</PointCollection>
</Grid.Resources>
<chartingToolkit:Chart Title="Chart Title">
<chartingToolkit:ColumnSeries Name="chart1" Title="Column Series" ItemsSource="{StaticResource sampleData}" IndependentValueBinding="{Binding X}" DependentValueBinding="{Binding Y}"/>
</chartingToolkit:Chart>
</Grid>
and with this code-behind:
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var element = Mouse.DirectlyOver as DependencyObject;
while (element != null && !(element is ColumnDataPoint))
{
element = VisualTreeHelper.GetParent(element);
}
if (element != null)
{
var columnDataPoint = element as ColumnDataPoint;
Debug.WriteLine("X = " + columnDataPoint.IndependentValue);
Debug.WriteLine("Y = " + columnDataPoint.DependentValue);
}
}
the X and Y values for the item the mouse is over will be printed out when the left mouse button is clicked.
Here is the example code that works.
MainWindow.xaml:
<Window x:Class="ColumnSeriesApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:local="clr-namespace:ColumnSeriesApp"
Title="Pet Data" Height="350" Width="525">
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ColumnSeriesApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public PetData m_PetData;
public MainWindow()
{
m_PetData = new PetData();
DataContext = m_PetData;
InitializeComponent();
}
private void m_colserHistogram_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
// Figure out what column we are on and display a popup menu based on the information.
IInputElement ieMouseOver = e.MouseDevice.DirectlyOver;
Rectangle rMouseOver = (Rectangle)ieMouseOver;
string strMouseOverContext= rMouseOver.DataContext.ToString();
string strMouseOverKey= "";
foreach (var vKvP in m_PetData)
{
if (1 == strMouseOverContext.IndexOf(vKvP.Key))
strMouseOverKey = vKvP.Key;
}
if (!String.IsNullOrEmpty(strMouseOverKey))
MessageBox.Show("The X value is " + strMouseOverKey);
}
}
public class PetData : Dictionary<string, int>
{
public PetData()
{
Add("SallyBeagle", 7);
Add("Cujo", 10);
Add("DobyDeedle", 11);
Add("Caramel", 6);
Add("Boo", 6);
}
}
}
It seems to work pretty well. If Rick had not come back with an idea, I would probably have quit looking for awhile - thanks for the motivation!
Now - is this solution all MVVM and whatnot? It still feels a little like a hack....

Categories