I've recently had a go at embedding a PowerPoint file as an XpsDocument in WPF.
It is a simple WPF application in which I embed a DocumentViewer property into my MainWindow.xaml grid:
<Window x:Class="PowerPoint2.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:PowerPoint2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DocumentViewer
Name="DocumentviewPowerPoint"
VerticalAlignment="Top"
HorizontalAlignment="Left" />
</Grid>
To create the document bound to "DocumentviewPowerPoint" I convert the PowerPoint file that has been opened into Xps format and bind this variable to the XAML property mentioned earlier:
using System;
using System.IO;
using System.Windows;
using System.Windows.Xps.Packaging;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using Application = Microsoft.Office.Interop.PowerPoint.Application;
namespace PowerPoint2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
const string powerPointFile = #"c:\temp\ppt.pptx";
var xpsFile = Path.GetTempPath() + Guid.NewGuid() + ".pptx";
var xpsDocument = ConvertPowerPointToXps(powerPointFile, xpsFile);
DocumentviewPowerPoint.Document = xpsDocument.GetFixedDocumentSequence();
}
private static XpsDocument ConvertPowerPointToXps(string pptFilename, string xpsFilename)
{
var pptApp = new Application();
var presentation = pptApp.Presentations.Open(pptFilename, MsoTriState.msoTrue, MsoTriState.msoFalse,
MsoTriState.msoFalse);
try
{
presentation.ExportAsFixedFormat(xpsFilename, PpFixedFormatType.ppFixedFormatTypeXPS);
}
catch (Exception ex)
{
MessageBox.Show("Failed to export to XPS format: " + ex);
}
finally
{
presentation.Close();
pptApp.Quit();
}
return new XpsDocument(xpsFilename, FileAccess.Read);
}
}
}
This all works well enough when running the program, showing the Xps document embedded into the WPF:
My question is how can I further modify my code in order to display the PowerPoint not just as a series of scrollable slides as shown, but as an actual slide show? I would like to make further updates to enable the user to navigate to the following slide on each mouse click - like a 'proper' presentation. My problem is that I am unfamiliar with the usage of the XpsDocument Apis - I don't know if it's a case of using these to achieve what I want or is it in the settings properties of the presentation variable that gets converted to the Xps format.
I managed to solve the particular problem I was interested in.
Please refer to this blog posting for a detailed explanation:
Controlling DocumentViewer methods and properties using MVVM
The solution addresses the problem of being able to enable individual PowerPoint slides (converted to xps file format) to occupy the WHOLE of the available windows space by invoking the relevant combination of DocumentViewer methods.
On pressing the screen button to invoke the RelayCommand, the following combination of DocumentViewer API calls in the MainWindowViewModel.cs class was observed to work:
public ICommand Command
{
get
{
return _command ?? (_command = new RelayCommand(
x =>
{
DocumentViewer = MainWindow.GetInstance();
const string powerPointFile = #"c:\temp\ppt.pptx";
var xpsFile = Path.GetTempPath() + Guid.NewGuid() + ".xps";
var xpsDocument = ConvertPowerPointToXps(powerPointFile, xpsFile);
FixedFixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
DocumentViewer.Document = FixedFixedDocumentSequence;
DocumentViewer.GoToPage(1);
DocumentViewer.FitToMaxPagesAcross(1);
WindowState = WindowState.Maximized;
DocumentViewer.FitToMaxPagesAcross(1);
}));
}
}
And to obtain the DocumentViewer instance itself? I also need to update the MainWindow.xaml.cs to get it to return the instance of the DocumentViewer object:
using System.Windows.Controls;
namespace DocumentView
{
public partial class MainWindow
{
private static DocumentViewer _docViewer;
public MainWindow()
{
InitializeComponent();
_docViewer = DocumentViewPowerPoint;
}
public static DocumentViewer GetInstance()
{
return _docViewer;
}
}
}
Where DocumentViewPowerPoint is the name given to the DocumentViewer in the MainWindow.xaml:
<Window x:Class="DocumentView.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:DocumentView"
mc:Ignorable="d"
WindowState="{Binding WindowState, Mode=TwoWay}"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<DocumentViewer
Grid.Row="0"
Document="{Binding FixedFixedDocumentSequence}"
Name="DocumentViewPowerPoint"
VerticalAlignment="Top"
HorizontalAlignment="Left" />
<Button
Grid.Row="1"
Command="{Binding Command}"
Width="70" Height="30" Content="Press" />
</Grid>
Related
I have excel where in Sheet 1 i have 2 tables ,i want to read data by infragistics WPF and bind in UI below is the structure of data in excel , i am new to WPF can anyone help
One of the methods to read the Excel file is using the XamSpreadsheet control that allows visualizing and editing of spreadsheet data.
There is the code example.
using Infragistics.Documents.Excel;
using System;
using System.IO;
using System.Windows;
using System.Windows.Resources;
namespace ReadExcelFile
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadFile();
}
private void LoadFile()
{
// For this example the excel document is located in resources
var fileName = "/ReadExcelFile;component/Resources/Book1.xlsx";
Stream stream = GetFileAsStream(fileName);
if (Workbook.Load(stream) is Workbook wb)
{
this.xamSpreadsheet1.Workbook = wb;
this.Title = "Book1.xlsx";
}
}
public static Stream GetFileAsStream(string fullName)
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri(fullName, UriKind.Relative));
return sri.Stream;
}
}
}
The XAML:
<Window x:Class="ReadExcelFile.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:ig="http://schemas.infragistics.com/xaml"
mc:Ignorable="d"
Height="450" Width="800">
<Grid>
<ig:XamSpreadsheet x:Name="xamSpreadsheet1" />
</Grid>
</Window>
The application will looks like on the screenshot:
It is necessary add references to the following assemblies:
InfragisticsWPF4.Controls.Grids.XamSpreadsheet
InfragisticsWPF4.Documents.Excel
For additional information see: XamSpreadsheet
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;
}
}
}
I'm trying to load an image from an url in WPF in a very simple way, but it's not working. Any help ? The code is below :
Main XAML
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="279*"></RowDefinition>
<RowDefinition Height="41*">
</RowDefinition>
</Grid.RowDefinitions>
<Image x:Name="image1" Grid.Row="0"></Image>
<TextBox Grid.Row="1" Margin="0,0,10,0"></TextBox>
</Grid>
</Window>
Code-behind
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri("http://www.clipartkid.com/images/817/pic-of-german-flag-clipart-best-VkuN37-clipart.jpeg");
bi.EndInit();
//var img = new Image();
image1.Source = bi;
}
}
Funny indeed! Works alright at my other laptop now. Must be something with the firewall settings. And yeps, I've made things even simpler now. Binding the source of the image in XAML to just a string property I've set in a ViewModel class.
<Image x:Name="image1" Source ="{Binding MyPic}" Grid.Row="0"></Image>
class MyViewModel
{
public string MyPic {
get { return #"http://www.clipartkid.com/images/817/pic-of-german-flag-clipart-best-VkuN37-clipart.jpeg"; }
}
}
Thanks for the responses, and sorry for the confusion.
-Ron
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;
}
}
}
I'm trying to make KinectColorViewer to work with SDK 1.7 but without success. I can display video picture only if I manually copy pixels from camera to Image element.
I have the following XAML:
<Page
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:k="http://schemas.microsoft.com/kinect/2013"
xmlns:WpfViewers="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers" x:Class="KinectD.Camera"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="500"
Title="Camera">
<Grid>
<k:KinectUserViewer k:KinectRegion.KinectRegion="{Binding ElementName=kinectRegion}" Height="100" HorizontalAlignment="Center" VerticalAlignment="Top" />
<k:KinectSensorChooserUI HorizontalAlignment="Center" VerticalAlignment="Top" x:Name="sensorChooserUi" />
<k:KinectRegion x:Name="kinectRegion">
<Grid>
<k:KinectCircleButton Label="Menu" HorizontalAlignment="Right" Height="200" VerticalAlignment="Top" Click="MenuButtonOnClick" >
<StackPanel>
<Image Source="Images/smile.png" Height="30"/>
</StackPanel>
</k:KinectCircleButton>
</Grid>
</k:KinectRegion>
<WpfViewers:KinectColorViewer HorizontalAlignment="Left" Height="240" Margin="10,10,0,0" VerticalAlignment="Top" Width="320" Kinect="{Binding ElementName=sensorChooserUi, Mode=OneWay, Path=Kinect}"/>
</Grid>
And XAML.CS:
public partial class Camera : Page
{
#region "Kinect"
private KinectSensorChooser sensorChooser;
#endregion
public Camera()
{
this.InitializeComponent();
// initialize the sensor chooser and UI
this.sensorChooser = new KinectSensorChooser();
//Assign the sensor chooser with the sensor chooser from the mainwindow.
//We are reusing the sensorchoosing declared in the first window that can in contact with kinect
this.sensorChooser = Generics.GlobalKinectSensorChooser;
//subscribe to the sensorChooserOnKinectChanged event
this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged;
//Assign Kinect Sensorchooser to the sensorchooser we got from our static class
this.sensorChooserUi.KinectSensorChooser = sensorChooser;
// Bind the sensor chooser's current sensor to the KinectRegion
var regionSensorBinding = new Binding("Kinect") { Source = this.sensorChooser };
BindingOperations.SetBinding(this.kinectRegion, KinectRegion.KinectSensorProperty, regionSensorBinding);
}
private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args)
{
bool error = false;
if (args.OldSensor != null)
{
try
{
args.OldSensor.DepthStream.Range = DepthRange.Default;
args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false;
args.OldSensor.DepthStream.Disable();
args.OldSensor.SkeletonStream.Disable();
args.OldSensor.ColorStream.Disable();
}
catch (InvalidOperationException)
{
// KinectSensor might enter an invalid state while enabling/disabling streams or stream features.
// E.g.: sensor might be abruptly unplugged.
error = true;
}
}
if (args.NewSensor != null)
{
try
{
args.NewSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
args.NewSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
args.NewSensor.SkeletonStream.Enable();
}
catch (InvalidOperationException)
{
error = true;
// KinectSensor might enter an invalid state while enabling/disabling streams or stream features.
// E.g.: sensor might be abruptly unplugged.
}
}
if (!error)
kinectRegion.KinectSensor = args.NewSensor;
}
private void MenuButtonOnClick(object sender, RoutedEventArgs e)
{
//Unsubscribe to the sensorchooser's event SensorChooseronkinectChanged
this.sensorChooser.KinectChanged -= SensorChooserOnKinectChanged;
(Application.Current.MainWindow.FindName("_mainFrame") as Frame).Source = new Uri("MainMenu.xaml", UriKind.Relative);
}
}
In tutorial I was following (http://channel9.msdn.com/Series/KinectQuickstart/Camera-Fundamentals) instructor just drops KinectColorViewer to a screen, sets path and it's working.
The KinectColorViewer available in the KinectWpfViewers assembly is used in the "Kinect Explorer" example, and that is the best place to see how it is used and behaves. From this example you will find that the proper way to initialize the viewer in the XAML and the bindings that are necessary.
From the code you posted, you appear to binding the Kinect itself (a reference to the hardware) to the KinectColorViewer, which is not what it is expecting. You need to be setting a reference to a KinectSensorManager class, which is part of the KinectWpfViewers assembly.
Here is a simplified XAML with a KinectColorViewer
<Window x:Class="Microsoft.Samples.Kinect.KinectExplorer.KinectWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Microsoft.Samples.Kinect.KinectExplorer"
xmlns:kt="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers"
Title="Kinect Explorer" Width="812" Height="768">
<Grid>
<kt:KinectColorViewer x:Name="ColorViewer" KinectSensorManager="{Binding KinectSensorManager}" CollectFrameRate="True" RetainImageOnSensorChange="True" />
</Grid>
</Window>
Then you XAML.CS constructor will look something similar to:
public KinectWindow()
{
this.viewModel = new KinectWindowViewModel();
// The KinectSensorManager class is a wrapper for a KinectSensor that adds
// state logic and property change/binding/etc support, and is the data model
// for KinectDiagnosticViewer.
this.viewModel.KinectSensorManager = new KinectSensorManager();
Binding sensorBinding = new Binding("KinectSensor");
sensorBinding.Source = this;
BindingOperations.SetBinding(this.viewModel.KinectSensorManager, KinectSensorManager.KinectSensorProperty, sensorBinding);
// Attempt to turn on Skeleton Tracking for each Kinect Sensor
this.viewModel.KinectSensorManager.SkeletonStreamEnabled = true;
this.DataContext = this.viewModel;
InitializeComponent();
}
Review the "Kinect Explorer" example for complete details.
I had the same problem with using KinectSensorManager. It turns out you need to call to Microsoft.Samples.Kinect.Wpf.Viewers and not just the kinect.dll, kinect.toolkit.dll, and kinect.controls.dll.
However, when I implemented KinectSensorManager, then the KinectUI and SensorChooser stopped working on my xaml interface.