First of all, I work in C# / XAML on Visual Studio C# 2013 Express.
I write today for a swf player in a wpf application. I read many discuss on this subjetcs but no one can solve my problem. I use a PageSwitcher, each page inherit from UserControl class and I create a button for each .swf file in a directory. This work fine. When I click on one of them, I want to open a new page wich is the swf viewer. I can open a new page, but my pdf viewer not work. I tried many ways to resolve my problem, but nothing work. I have a WinForm application wich do what I want, but I can't add it in my WPF application I try a WindowsFormHost but I still have the same error : Type or namespace 'AxShockwaveFlashObjects' not found, whereas I add AxInterop.ShockwaveFlashObjects.dll and/or Interop.ShockwaveFlashObjects.dll
Here is my code :
XAML
<UserControl x:Class="WPFPageSwitch.swf"
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:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
mc:Ignorable="d">
<Grid x:Name="Container">
</Grid>
</UserControl>
C#
namespace WPFPageSwitch
{
public partial class swf : System.Windows.Controls.UserControl, ISwitchable
{
public swf(string swf)
{
InitializeComponent();
System.Windows.Forms.Integration.WindowsFormsHost swfPlayer = new System.Windows.Forms.Integration.WindowsFormsHost();
AxShockwaveFlashObjects.AxShockwaveFlash axShockwaveFlash = new AxShockwaveFlashObjects.AxShockwaveFlash();
swfPlayer.Child = axShockwaveFlash;
Container.Children.Add(swfPlayer);
}
}
}
Somebody can help me ?
The error is here : AxShockwaveFlashObjects.AxShockwaveFlash axShockwaveFlash = new AxShockwaveFlashObjects.AxShockwaveFlash();
Add a reference to it.
AxShockwaveFlashObjects is a COM library to allow you to integrate Flash content into your .NET application. Demo Here
Related
UPDATE * My error had to do with the computer I was using and not being part of a FIPS validated algorithm. That error only appeared after I closed Visual Studio and tried to compile the default blank WPF form. I did the exact same thing on a personal computer and it compiled as expected. *
I'm familiar with creating windows form using C#. I looked into methods to change the look of my forms, similar to using skins, and was told that it would be easier if I I used WPF....ok.
In an effort to become familiar with WPF, I picked up a book, "MASTERING_WINDOWS_PRESENTATION_FOUNDATION" and it was slow moving with a discussion on MVVM and data binding (new topics to me). I felt the I could learn the difference between WPF and Windows Forms much faster if I first tried to create a simple WPF application. Then, as I read, I could see how something done in a very familiar way using Windows Forms, is done using WPF.
Unfortunately, I'm stuck right out the box!
Using VS2017, I created a new WPF App (.NET Framework) I then added a text box and a button. I created a name for both as this does not appear to be automatically created like with Windows Forms. I then double click on the button and a method block is created in the MainWindow.xaml.cs file. I proceed to add text to the textbox
(txtbox_1.Text="Hello;").
I noticed a few things:
1.) The InitializeComponent(); call in the MainWindow() method is underlined and corresponds to the CS0103 Error
2.) Intellisense did not recognize the textbox. I typed out the full name and it created an error when I was done. (Same CS0103 Error)
I looked through stackoverflow but found articles about Xamarin. I've heard of this as a way to write code for Android but do not know how it relates to what I'm trying to do.
What am I missing?
Here is my XAML file:
<Window x:Class="WpfApp4.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:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button x:Name="btn_browse" Content="Button" HorizontalAlignment="Left" Margin="524,91,0,0" VerticalAlignment="Top" Width="75"/>
<TextBlock x:Name="txtbox_1" HorizontalAlignment="Left" Margin="134,93,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="20" Width="363"/>
</Grid>
</Window>
And here is my MainWindow.xaml.cs file:
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void SimpleMethod()
{
txtbox_1.Text = "Hello";
}
}
}
That InitializeComponent(); line is hugely significant since it is that instruction makes your xaml turn into UI.
If it's underlined then your partial class for code behind isn't matching up with the xaml.
You will have something like
Either that or you're missing some fundamental references should have been added when you started your project.
I suggest you just bin your sln though.
Just delete it and start again.
Create a new wpf project.
Hit f5.
If it crashes and burns your VS install is broken.
Anyone and everyone will tell you to learn MVVM. And you should.
https://social.technet.microsoft.com/wiki/contents/articles/31915.wpf-mvvm-step-by-step-1.aspx?Redirected=true
https://social.technet.microsoft.com/wiki/contents/articles/32164.wpf-mvvm-step-by-step-2.aspx?Redirected=true
That means that your MainWindow class actually does not inherit from System.Windows.Window class.
I don't see a reference to System.Windows in your class. This is the namespace, where the WPF Window basic class is defined. So your Window base class can reference a Window class from some other namespace, and, that's why it has no InitializeComponents() method and can not reference the property from XAML. Try first adding this using directive to code behind class. If that doesn't do the task, check if your project has a reference to System.Windows. If there is no System.Windows in your reference manager (this could be if your project is set to compile in .NET 4.0) add references to PresentationFramework, PresentationCore and WindowsBase.
The error had to do with the work computer I was using ... something to do with FIPS. You should be able to create a new WPF project and compile the blank form without error. I did the exact same thing on a personal computer and it compiled as expected. I was also able to reproduce a WinForms project as a WPF using code behind without error. Implementation using MVVM took a significant amount of time since the concept of view models was new to me but, in the end, it's not bad.
I created a user control in a project that consists of just a MainWindow.xaml and the code behind. I added the .dll to the toolbox of VS, and dropped it onto a window in a new project. This created the following:
<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:WpfApplication5"
xmlns:ThinkGeoClean="clr-namespace:ThinkGeoClean;assembly=ThinkGeoClean" x:Class="WpfApplication5.MainWindow"
mc:Ignorable="d"
Title="MainWindow" >
<Grid>
<ThinkGeoClean:ListBoxCustom x:Name="listBoxCustom" />
</Grid>
</Window>
The ThinkGeoClean is the name of the .dll I added which is the usercontrol. ListBoxCustom is just a public class in the control, but is NOT what I want to show. I want to show the main window of the usercontrol (not a window), but it doesn't show as an option after typing <ThinkGeoClean:. The only thing that shows up is ListBoxCustom. If I go ahead and type <ThinkGeoClean.MainWindow>, it gives a XamlParseException error on that line.
Now, if I go into the code-behind and do:
ThinkGeoClean.MainWindow newWin = new ThinkGeoClean.MainWindow();
newWin.Show();
It will pop up the usercontrol in a new window and it works fine.
Here's the beginning of usercontrol code behind:
namespace ThinkGeoClean
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
And the xaml for it is just a single grid containing some buttons and a map control.
Edit: In addition to the answer below, my user control was originally just a normal WPF project. I thought changing the output type to a class library would change it into a user control automatically, but I actually had to go into the xaml and change the the into .
This XAML:
<Grid>
<ThinkGeoClean:ListBoxCustom x:Name="listBoxCustom" />
</Grid>
...is not equivalent to creating an instance of a window and call the Show() method on it programmatically.
Instead the XAML processor will try to add the window to the Children collection of the Grid and this is not possible since a window cannot be child of another control. That's why you get an exception.
Also, a UserControl must be hosted in a window or page. It is not a top-level control that you can display without any parent host.
I need to embed a standard webapp inside a windows app container.
The container just needs to be a wrapper around a webkit or similar rendering engine (I would like to avoid using IE rendering engine if possible) and it would contain some minimal window management logic - things like being borderless with no titlebars, being able to fix position and size, and custom overlay/always-on-top rules based on currently focused window.
I've been looking at node-webkit and it seems to fit the bill as far as containing a webapp is concerned, but I'm not sure I would be able to do the latter.
Can this be done with node-webkit, is there some other approach that would fit my use case better? I have absolutely no idea how windows app development is done, so I would appreciate some help.
If you are using WPF, you can create you window with something like this (xaml):
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None"
Title="MainWindow" Left="100" Top="200" Height="350" Width="525" PreviewKeyDown="Window_PreviewKeyDown">
<Grid x:Name="grdBrowserHost">
</Grid>
</Window>
WindowStyle attr None means "borderless with no titlebars" window. Left and Top are for position, and Width and Height are self-explanatory. All those properties can be accessed via code-behind with simple this.Width, etc... PreviewKeyDown I put here because in this example (as you will see), Topmost property will be changed from code behind (dynamically).
Code-behind should look something like
using System;
using System.Windows;
using System.Windows.Input;
namespace WpfApplication2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
System.Windows.Controls.WebBrowser browser = new System.Windows.Controls.WebBrowser();
// you can put any other uri here, or better make browser field and navigate to desired uri on some event
browser.Navigate(new Uri("http://www.blic.rs/"));
grdBrowserHost.Children.Add(browser);
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
this.Close();
}
else
{
this.Topmost = !this.Topmost;
}
}
}
}
In this example I have created default WebBrowser control for showing html. If you want some other web rendering engine you must find 3rd-part control and include it in your references. For webkit, you can check How to use WebKit browser control in WPF
What you need is "UWP Bridge for web apps" ;-)
Look in the Windows Dev Center:
UWP Bridge for web apps - Create your app now
Well, This has been solved. I don't know if it was a glitch on my end or if ImageTools was just a pain to set up accordingly. Thank you for the answers everyone, and they very likely all work. The once I marked as answered does indeed work, BUT HERE IS HOW:
(Credits go to Patrick for sticking with this, and his code is used as
noted below.)
(Also, big thanks to everyone else too who submitted something. Sorry
if my noobness scared you all away)
To get an animated Gif to work in your wp7 7.1 app, do these steps:
1) Download ImageTools (I used latest version at the time (0.3)) http://imagetools.codeplex.com/downloads/get/156530
2) Unstuff the file, and in the "Bin > Phone" folder just throw ALL* the dll file extensions into your wp7 app folder. The other files (xml/pdb) don't need to be added. (*this step is extra work, and we will be removing these extra dlls later on, but hell it'll save a headache.)
3) Add the references to your wp7 app in the Solution Explorer window > References folder drop down. To do that, right click the References folder, click "Add Reference" and browse to the dll files. Repeat this process. (Referencing all those dlls is 1 minute of the extra 2 mins of work ultimately, but you shouldn't get any errors when compiling)
4) Now on the xaml page you want to add the image to, add this at the top in your header code:
xmlns:it="clr-namespace:ImageTools.Controls;assembly=ImageTools.Controls"
xmlns:vm="clr-namespace:GifViewer.ViewModels"
NOTICE: Change "GifViewer" to your application name.
5) On that same page, just below it, add in this code:
<phone:PhoneApplicationPage.DataContext>
<vm:MainViewModel/>
</phone:PhoneApplicationPage.DataContext>
<Grid x:Name="LayoutRoot" Background="Transparent">
<it:AnimatedImage Source="{Binding AnimationImage}" />
</Grid>
NOTICE: Doesn't have to be a grid. Doesn't have to be in anything at all. It can stand alone.
6) The accepted answer has a folder in the app called "ViewModels" and in it is a custom class titled "MainViewModel" So in the solution explorer or in the desktop, add a folder called ViewModels and make a c# class page titled MainViewModel. Move that into that folder, and refresh the solution explorer. If you cannot see the file, you need to click the "Show all files" button just under the Solution Explorer Header.
7) Using the accepted answer below, in the "MainViewModel.cs" class page, add the following just below the others:
using ImageTools;
using ImageTools.Controls;
using ImageTools.IO;
using ImageTools.IO.Gif;
8) The accepted answer uses this code. Change "GifViewer" to your application name when copying this code, and also change the Uri location of the gif. In my example, I have a folder named "Gif" and in it is "explosion.gif". Build Action can be kept as Resource by default.
namespace GifViewer.ViewModels {
public class MainViewModel : DependencyObject {
public MainViewModel() {
Decoders.AddDecoder<GifDecoder>();
Uri uri = new Uri("Gif/explosion.gif", UriKind.Relative);
ExtendedImage image = new ExtendedImage();
// either of these two method work.
// Just remove the first / to switch
//*
image.LoadingCompleted +=
(o, e) => Dispatcher.BeginInvoke(() => AnimationImage = image);
image.UriSource = uri;
/*/
Stream stream = Application.GetResourceStream(uri).Stream;
GifDecoder decoder = new GifDecoder();
decoder.Decode(image, stream);
AnimationImage = image;
/**/
}
public static readonly DependencyProperty AnimationImageProperty =
DependencyProperty.Register("AnimationImage",
typeof(ExtendedImage),
typeof(MainViewModel),
new PropertyMetadata(default(ExtendedImage)));
public ExtendedImage AnimationImage {
get { return (ExtendedImage)GetValue(AnimationImageProperty); }
set { SetValue(AnimationImageProperty, value); }
}
}
}
Go ahead and compile. You might get a runtime error indicating that
couldn't load, but running the application should get rid of it.
Your gif should now play.
9) Start removing the EXTRA dll references which aren't needed (the other minute of the 2 mins of extra work). Basically, all you want referenced is:
ImageTools
ImageTools.Controls
ImageRools.IO.Gif
ImageTools.Controls might not even be needed, but the file size is like 25kb and honestly I couldn't get the gif to show if I removed it.
There you go!
Original Question I asked
I am having the worst headache trying to get my animated gif to play in my WP7 app. I simply cannot connect the dots to make this happen, despite having imagetools and viewing the current "solutions" on stackoverflow/the web.
My problem is outlined below, but for reference I have looked at:
Display GIF in a WP7 application with Silverlight
and http://blog.naviso.fr/wordpress/?p=733
So how does one actually set this blasted thing up to display animated gifs in a wp7 app!? Question in specific -- Is this the correct code to get my animated gif to appear? If not, what below needs to be fixed?
My animated gif file location on the phone (NOT from internet):
Gif/explosion.gif
Main Xaml page:
xmlns:imagetools="clr-namespace:ImageTools.Controls;assembly=ImageTools.Controls"
....
<phone:PhoneApplicationPage.Resources>
<imagetools:ImageConverter x:Key="ImageConverter" />
</phone:PhoneApplicationPage.Resources>
....
<imagetools:AnimatedImage x:Name="animationgif" Source="{Binding ImageSource, Converter={StaticResource ImageConverter}}" />
and the code behind for the xaml page:
public partial class MainPage : PhoneApplicationPage
{
using ImageTools;
using ImageTools.Controls;
using ImageTools.IO.Gif;
public MainPage()
{
InitializeComponent();
ImageTools.IO.Decoders.AddDecoder<ImageTools.IO.Gif.GifDecoder>();
}
public void eventtofiretoshowexplosion_gif(object sender, MouseButtonEventArgs e)
{
// A main problem -- this code doesn't work by itself, as what is the ImageSource!?
// It cannot be used as a variable it says.
// animationgif.ImageSource does not work at all (not a method).
ImageSource = new Uri("http://mysite/my.gif", UriKind.Absolute);
}
}
This has been bugging me for the past few hours, and I really could use some help with this. If there is a quick fix to this, please help out and show how it is done instead of guiding me to a page. I've seen FAR too many pages about this, and while each one claims to work, it just cannot in my app.
It seems the Build Action of your image is incorrect, so the application can't "find" the resource.
You have two options. Either set the build action to content, or set it as resource and specify "copy if newer".
Open your solution explorer
Open your properties window
Click on your image
Set Build action to Resource and Copy to output directory Copy if newer
Further reading: Resources in WPF – I (Binary Resources)
I put together a small example using a viewmodel which seems to work given the above resource "requirement". I hope you might find it useful. The viewmodel class is in a folder called ViewModels.
using System;
using System.IO;
using System.Windows;
using ImageTools;
using ImageTools.IO;
using ImageTools.IO.Gif;
namespace GifViewer.ViewModels {
public class MainViewModel : DependencyObject {
public MainViewModel() {
Decoders.AddDecoder<GifDecoder>();
Uri uri = new Uri("Gif/explosion.gif", UriKind.Relative);
ExtendedImage image = new ExtendedImage();
// either of these two method work.
// Just remove the first / to switch
//*
image.LoadingCompleted +=
(o, e) => Dispatcher.BeginInvoke(() => AnimationImage = image);
image.UriSource = uri;
/*/
Stream stream = Application.GetResourceStream(uri).Stream;
GifDecoder decoder = new GifDecoder();
decoder.Decode(image, stream);
AnimationImage = image;
/**/
}
public static readonly DependencyProperty AnimationImageProperty =
DependencyProperty.Register("AnimationImage",
typeof(ExtendedImage),
typeof(MainViewModel),
new PropertyMetadata(default(ExtendedImage)));
public ExtendedImage AnimationImage {
get { return (ExtendedImage)GetValue(AnimationImageProperty); }
set { SetValue(AnimationImageProperty, value); }
}
}
}
and the xaml
<phone:PhoneApplicationPage
x:Class="GifViewer.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:it="clr-namespace:ImageTools.Controls;assembly=ImageTools.Controls"
xmlns:vm="clr-namespace:GifViewer.ViewModels"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.DataContext>
<vm:MainViewModel/>
</phone:PhoneApplicationPage.DataContext>
<Grid x:Name="LayoutRoot" Background="Transparent">
<it:AnimatedImage Source="{Binding AnimationImage}" />
</Grid>
</phone:PhoneApplicationPage>
When using AnimatedImage your source binding has to return ExtendedImage. ExtendedImage class is basically the "BitmapImage" equivalent of the normal Image control.
I am trying to allow several classes to inherit a more general Silverlight user control to avoid redundancy in my code. The classes inherit the extended control, which then inherits the User Control class. The issue I have been running into is that the ExtendedControlExtension.g.cs file regenerates every time I compile, with the incorrect inheritance (it inherits User Control not my Extended Control).
Note that I have been inheriting the Extended Control in the .cs and g.cs files, but continuing to use the User Control tag in the .aspx file as this causes the error
Error 29 The tag 'ExtendedControl' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
Is there a way to fix this?
Thanks!
You cannot change the .g.cs file, in fact is says so right in the file. Also, it's unfortunate to use the term "custom control" as this means something specific and not what you are trying to do. But, the good news is that what you are trying to do is possible.
Derive from UserControl:
public class FancyUserControl : UserControl
{
// Your added common functionality.
}
and then add a new UserControl to your project using the normal mechanism, let's say UserControl1. Then edit the UserControl.xaml files as follows:
<local:FancyUserControl x:Class="SilverlightApplication1.UserControl1"
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:SilverlightApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</local:FancyUserControl>
paying special attention to the three lines with local in them, adjusting to your application. Then edit the UserControl1.xaml.cs file as follows:
public partial class UserControl1 : FancyUserControl
{
public UserControl1()
{
InitializeComponent();
}
}
and Visual Studio won't be quite happy yet but finally rebuild your project and all will be well.
The class UserControl1 is now derived from FancyUserControl instead of UserControl and you can begin adding your common functionality. To add more controls you will need to manually edit the XAML and code-behind once after initially adding each new control to the project.