Trouble with camera in Windows Phone 8.0 using videobrush - c#

I got bug in my app and can not solve it.
Bug is - I can not use camera more than three times.
Code, that demonstrates it is here:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp1.Resources;
using System.Windows.Media;
using Microsoft.Devices;
namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{
PhotoCamera aCamera = new PhotoCamera(CameraType.Primary);
// Constructor
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var cam = new camera_VM();
}
}
class camera_VM
{
public camera_VM()
{
VideoBrush __cameraView = new VideoBrush();
PhotoCamera aCamera = new PhotoCamera(CameraType.Primary);
__cameraView.SetSource(aCamera);
}
}
}
On third launch - it throws error:
{System.NotSupportedException: Specified method is not supported.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.VideoBrush_SetExternalVideoPortSource(VideoBrush vb, String sPortName)
at Microsoft.Devices.Camera
Have no idea - what does it mean.
My guess that having Videobrush in code - bad idea, but have I another option? I got Grid in DataTemplate, that binds to VideoBrush object.
If I must properly dispose camera unit - can anyone tell me, how to do it right?
P.S. Code is just example, that fires an error.

Related

How to add ui frameworks to a c# form

overflow, im having trouble trying to add a ui framework , for exmple metro framework, to my c# form.
I know that i have to include the reference and include it with
using MetroFramework.Forms;
but im not sure what else to do here and when ever i try and google it, i just get websites that try and sell me a framework package.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Forms;
namespace FrameworkTest
{
public partial class Form1
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
I know this may be a silly question but any help would be much appreciated.
Thanks to Crowcoder I found out that I was supposed to add
MetroForm after Form1 like:
public partial class Form1 : MetroForm {
//Code goes here
}

AddChild() not accessible in RibbonQuickAccessToolBar

In the code below, the line 'QuickToolBar.AddChild(SaveBtn)' gives the following error:
System.Windows.Controls.ItemsControl.AddChild(object) is inaccessible
due to its protection level.
I don't understand the reason for this error message. Please help
using System;
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.Controls.Ribbon;
namespace Ribbon3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CreateRibbon();
}
public void CreateRibbon()
{
Ribbon ribbon = new Ribbon();
ribbon.SelectedIndex = 0;
RibbonQuickAccessToolBar QuickToolBar = new RibbonQuickAccessToolBar();
RibbonButton SaveBtn = new RibbonButton();
QuickToolBar.AddChild(SaveBtn);
}
}
}
The method is protected:
https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.addchild(v=vs.110).aspx
A protected member of a base class is accessible in a derived class only if the access occurs through the derived class type.
https://msdn.microsoft.com/en-us/library/bcd5672a.aspx
As said by Derek you cannot use QuickToolBar.AddChild(SaveBtn) due to protection level. But you still can add using Items.Add like this
QuickToolBar.Items.Add(SaveBtn);

C#, WPF, Having trouble opening a new window

I've looked through the list of similar questions for this topic and couldn't find any examples which deal with my particular problem.
I'd like to start with the disclaimer that I'm not far off an absolute beginner.
Opening up a window in WPF is quite easy, I've done it before in a previous project and it worked fine.
However, I am struggling to do it again in this new project (login form). I have two classes, MainWindow and CreateAccount.
MainWindow has the event trigger for opening up the CreateAccount window.
private void Button_Click(object sender, RoutedEventArgs e)
{
var account = new CreateAccount();
account.Show();
this.Close();
}
Researching how to open up a new window in WPF gave me snippets very much like this one.
What I want to happen is for, upon triggering the button event, the window I've designed with an account creation form to appear. What instead happens is a blank window pops up with what I can only assume are default dimensions and no border text.
I don't understand how this can be as I specified exactly what I wanted. I don't get any errors either.
The CreateAccount class is mostly just some if statements (I don't want to hunker down with it until I sort out the current issue) and I can't find anything that would cause issue.
Both classes inherit from Window. I took a guess at what might be wrong, thinking 'maybe its an inheritance problem' and so tried to make CreateAccount inherit from MainWindow, but that threw an error which I now understand. Right now I'm lost as to what the problem is and since I don't know that, I can't find out the solution.
Is there anything wrong with the code? Someone suggested that it might be a DataContext issue, but even after looking that up I'm struggling to understand it.
Thank you.
EDIT: Because a lot of people were asking for more code with CreateAccount.xaml.cs(I thought we were only allowed to post snippets):
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.Shapes;
namespace Login
{
/// <summary>
/// Interaction logic for CreateAccount.xaml
/// </summary>
public partial class CreateAccount : Window
{
public bool canProceedPass = false;
public bool canProceedUser = false;
public void MakeAccount()
{
InitializeComponent();
}
public void CheckTextInput()
{
if (NewUsername.Text != null && NewPassword.Text != null) {
canProceedUser = true;
}
else
{
canProceedUser = false;
MessageBox.Show("You haven't filled out all the required fields.");
}
}
public void CheckPassInput()
{
if (NewPassword.Text == ConfirmNewPassword.Text)
{
canProceedPass = true;
}else
{
return;
}
}
private void CreateAccountButton_Click(object sender, RoutedEventArgs e)
{
if (canProceedUser == true && canProceedPass == true)
{
//Add username and password to my SqlDb.
}
}
}
}
A constructor should have the same name as your Class with no return type, in your example the constructor should be:
public CreateAccount()
{
InitializeComponent();
}

Creating partial class for wpf functions and events

I've a WPF program which it's MainWindows.xaml.cs has many functions and events. I want to make it more readable so I moved some of the functions and events in a separate class named "partialClassMainWindow.cs". I moved all button events to it. now when I click on a button it must go to button_Click function in partialClassMainWindow but a new funciton is created in MainWindows.xaml.cs.
how can I prevent it?
for example consider this simple program:
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 WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//moved to partialClassMainWindow:
//private void button1_Click(object sender, RoutedEventArgs e)
//{
//}
}
}
partialClassMainWindow.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace WpfApplication3
{
public partial class MainWindow:Window
{
public void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "Hello World";
}
}
}
Don't think that you can avoid it. It looks like Visual studio just checks the code behind file and not any other files with more partial code. But the other guys are right MVVM is the way to go and normally you have little or none code behind. Only typical view stuff you would handle there, all the business logic goes into the viewmodel.
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
Is a good place to start reading about MVVM.

Windows Phone 7 C# error Attepting to call function failed

I am writing a test project "HelloPhone" in c# Windows Phone 7 and i am trying to use
a C++ DLL/clr. Well at execution i get an unhandled exception error reporting that attempt
to call the DLL function failed. I am not a C# programmer so here is my code:
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Microsoft.Phone;
using DldesAPI;
using System.Runtime.InteropServices;
namespace DldesAPI
{
public class DldesLib
{
[DllImport("DLDESLIB.dll", CharSet = CharSet.Auto)]
public static extern int GetVersionNumber();
// [DllImport("DLDESLIB.dll")]
// public static extern int EncryptFirst(byte *pSrc,int SrcLen,byte *pDst,byte *pKey,int iKLen,long *wa,bool bRand);
}
}
namespace HelloPhone
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void ClickMeButton_Click(object sender, RoutedEventArgs e)
{
int x = 0;
string Msg;
Msg=MessageTextBox.Text;
x = 1;
x = DldesLib.GetVersionNumber();
}
}
}
Could you please tell me what i do wrong?
Thnk you.
Spyros
You probably won't be too happy to hear this, but using p/invoke or a C++/CLR DLL is not supported in Windows Phone 7.
yea duder, this kinda stuff probably isn't supported on WP7 (as far as I know):
using System.Runtime.InteropServices;
[DllImport("DLDESLIB.dll", CharSet = CharSet.Auto)]
It's running on a compact version of the .net framework.
Are you trying to get the DeviceFirmwareVersion or DeviceHardwareVersion? Can you just use:
DeviceExtendedProperties.GetValue("DeviceFirmwareVersion").ToString();
http://msdn.microsoft.com/en-us/library/ff941122(v=VS.92).aspx

Categories

Resources