AddChild() not accessible in RibbonQuickAccessToolBar - c#

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);

Related

Api running async method in the background without the use of a button?

Okay, so I have this API that I want to load in the background of my app the second it starts up I do initialize the request. But I do not call the async task anywhere and I do not want to use a button for this. I have my own Class that will take the data that comes from the response but I just want it to be handed over to another method so I can use it there and not have to show the data to the user. How should I go about this? this is my code from both Mainwindow.xaml.cs and API.cs
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 TestAPi
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Api.InitializeClient("");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
namespace TestAPi
{
public class Api
{
private static HttpClient ApiClient { get; set;}
private string url { get; set;}
public static void InitializeClient(string token)
{
ApiClient = new HttpClient();
ApiClient.BaseAddress = new Uri("your url");
ApiClient.DefaultRequestHeaders.Accept.Clear();
ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("Bearer Authentication"));
}
public async Task<Data> LoadData()
{
url = ApiClient.BaseAddress.ToString();
using (HttpResponseMessage response = await ApiClient.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
Data data = await response.Content.ReadAsAsync<data>();
return data;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
}
}
In your viewmodel, create a command that calls the method and uses the result
public AsyncCommand LoadCommand { get; }
// in constructor
LoadCommand = new AsyncCommand(LoadAndUse);
In XAML, hook the command to the Loaded event of the window
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
It looks like the async producer consumer design pattern could be useful in your use case scenario, and to support that you can consider the usage of Channels available on dotnet core framework.
Your producer would be the Class that would do the http request, and after that it should post the response in the a channel so the consumer class takes that data and do whatever it needs to do with that.
Here is a simple example to understand the concepts arround Producer Consumer and Channels.

Trouble with camera in Windows Phone 8.0 using videobrush

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.

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 "Error 24 Type X' already defines a member called 'X' with the same parameter type"

I have got the error :
Error 24 Type 'X.volSays' already defines a member called 'volSays' with the same parameter types
The code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace X
{
public partial class volSays : PhoneApplicationPage
{
public volSays()
{
InitializeComponent();
}
}
}
How do I fix this error ?

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