How to access form elements from another classes? - c++-cli

This my first time trying to create windows application using vs clr and i'm trying to implement it on my existing cpp project which i almost finished but the main problem that i'm facing is the i can't access the functions of myForm.h (buttons , text box , etc..) from my main source.cpp , any help would be appreciated and thanks
MyForm.h
#pragma once
namespace Project2 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::RichTextBox^ richTextBox1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(104, 177);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
//
// richTextBox1
//
this->richTextBox1->Location = System::Drawing::Point(47, 36);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(208, 135);
this->richTextBox1->TabIndex = 1;
this->richTextBox1->Text = L"";
this->richTextBox1->TextChanged += gcnew System::EventHandler(this, &MyForm::richTextBox1_TextChanged);
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(282, 253);
this->Controls->Add(this->richTextBox1);
this->Controls->Add(this->button1);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->ResumeLayout(false);
}
#pragma endregion
public: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {};
public: System::Void richTextBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {}
};
}
Main.c
#include <iostream>
#include <fstream>
#include <sstream>
#include <locale>
#include <string>
#include<vector>
#include <time.h>
#include "MyForm.h"
using namespace std;
using namespace System;
[STAThread]
int main()
{
System::Windows::Forms::Application::Run(gcnew Project2::MyForm());
MyForm F; \\ can't create an object of MyForm class
F.richtextbox->AppendText("Hello world"); \\ here is the confusing part
System::Windows::Forms::Application::Run(gcnew Project2::MyForm());
return 0;
}

Related

Windows phone 8 file storage C++

i'm beginning in C++ Windows phone.
Currently i'm doing a windows phone app with many pages and i don't know how to save and load my data. I tried global variables and SQLite database but i always meet errors so i tried " Windows::Storage::ApplicationDataContainer " but i can't pass the variable between all pages.
is there someone who have a template that use ApplicationDataContainer or someone who could explain me how to use it?
here is my code
MainPage.xaml.cpp
#include "pch.h"
#include "MainPage.xaml.h"
#include "menu.xaml.h"
using namespace japanEasy;
using namespace Windows::Storage;
using namespace Platform;
using namespace std;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Interop;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Popups;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
ApplicationDataContainer^ localSettings = ApplicationData::Current->LocalSettings;
// Pour en savoir plus sur le modèle d'élément Page vierge, consultez la page http://go.microsoft.com/fwlink/?LinkId=234238
MainPage::MainPage()
{
InitializeComponent();
}
/// <summary>
/// Invoqué lorsque cette page est sur le point d'être affichée dans un frame.
/// </summary>
/// <param name="e">Données d'événement décrivant la manière dont l'utilisateur a accédé à cette page. La propriété Parameter
/// est généralement utilisée pour configurer la page.</param>
void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
(void) e; // Paramètre non utilisé
}
void japanEasy::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
if (this->theName->Text == "" || this->theName->Text == nullptr)
{
auto msgDlg = ref new MessageDialog("Vous devez d'abord saisir votre nom", "Attention");
msgDlg->ShowAsync();
}
else
{
auto values = localSettings->Values;
values->Insert("playerName", dynamic_cast<PropertyValue^>(PropertyValue::CreateString(this->theName->Text)));
String^ value = safe_cast<String^>(localSettings->Values->Lookup("playerName"));
this->Frame->Navigate(TypeName(japanEasy::menu::typeid), localSettings);
}
}
menu.xaml.cpp
#include "pch.h"
#include "menu.xaml.h"
#include "learn.xaml.h"
#include "level.xaml.h"
#include "records.xaml.h"
#include "easyGame.xaml.h"
using namespace japanEasy;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Interop;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::Storage;
ApplicationDataContainer^ localSettings ;
menu::menu()
{
InitializeComponent();
this->myName->Text = safe_cast<String^>(localSettings->Values->Lookup("playerName"));
}
/// <summary>
/// Invoqué lorsque cette page est sur le point d'être affichée dans un frame.
/// </summary>
/// <param name="e">Données d'événement décrivant la manière dont l'utilisateur a accédé à cette page.
/// Ce paramètre est généralement utilisé pour configurer la page.</param>
void menu::OnNavigatedTo(NavigationEventArgs^ e)
{
(void) e; // Paramètre non utilisé
auto localSettings = (ApplicationDataContainer^)e->Parameter;
::Windows::UI::Xaml::Controls::Page::OnNavigatedTo(e);
}
void japanEasy::menu::goToLearn_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
this->Frame->Navigate(TypeName(japanEasy::learn::typeid));
}
void japanEasy::menu::goTo_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
}
void japanEasy::menu::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
this->Frame->Navigate(TypeName(japanEasy::level::typeid));
}
void japanEasy::menu::goToRecords_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
this->Frame->Navigate(TypeName(japanEasy::records::typeid));
}
when i launch my application on my windows phone i seize my username and i click on my button to show the "menu.xaml" page, and i'm supposed to have get the name in this page thanks to the "localSettings" i've passed on my MainPage.xaml.cpp with the naavigate function.
To pass data from a page to another i used "Page::OnNavigatedTo" method.
So on my menuPage I created a global variable and i added a menu::OnNavigatedTo()
where i get data from previous pages.
here is my menuPage.xaml.cpp
#include "pch.h"
#include "menu.xaml.h"
#include "learn.xaml.h"
#include "level.xaml.h"
#include "records.xaml.h"
#include "easyGame.xaml.h"
#include "mediumGame.xaml.h"
#include "hardGame.xaml.h"
#include "Constants.h"
using namespace japanEasy;
using namespace Platform;
using namespace std;
using namespace Windows::UI::Popups;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Interop;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::Phone::UI::Input;
using namespace Windows::Storage;
ApplicationDataContainer^ menuLocalSetting;
String^ lv = "Facile";
// Create a composite setting
ApplicationDataCompositeValue^ recordList = ref new ApplicationDataCompositeValue();
menu::menu()
{
InitializeComponent();
HardwareButtons::BackPressed += ref new EventHandler<BackPressedEventArgs ^>(this, &menu::HardwareButtons_BackPressed);
}
/// <summary>
/// Invoqué lorsque cette page est sur le point d'être affichée dans un frame.
/// </summary>
/// <param name="e">Données d'événement décrivant la manière dont l'utilisateur a accédé à cette page.
/// Ce paramètre est généralement utilisé pour configurer la page.</param>
void menu::OnNavigatedTo(NavigationEventArgs^ e)
{
//(void) e; // Paramètre non utilisé
if (e->Parameter) {
menuLocalSetting = safe_cast<ApplicationDataContainer^>(e->Parameter);
this->myName->Text = safe_cast<String^>(menuLocalSetting->Values->Lookup("userName"));
auto values = menuLocalSetting->Values;
if (safe_cast<String^>(menuLocalSetting->Values->Lookup("level"))) {
String^ value = safe_cast<String^>(menuLocalSetting->Values->Lookup("level"));
lv = value;
}
else {
auto vvv = menuLocalSetting->Values;
vvv->Remove("level");
vvv->Insert("level", dynamic_cast<PropertyValue^>(PropertyValue::CreateString("Facile")));
String^ test3 = safe_cast<String^>(menuLocalSetting->Values->Lookup("level"));
}
}
}
void japanEasy::menu::goToLearn_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
this->Frame->Navigate(TypeName(japanEasy::learn::typeid), menuLocalSetting);
}
void japanEasy::menu::goTo_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
if(lv == "Facile")
this->Frame->Navigate(TypeName(japanEasy::easyGame::typeid), menuLocalSetting);
else if(lv =="Moyen")
this->Frame->Navigate(TypeName(japanEasy::mediumGame::typeid), menuLocalSetting);
else if(lv == "Difficile")
this->Frame->Navigate(TypeName(japanEasy::hardGame::typeid), menuLocalSetting);
}
void japanEasy::menu::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
this->Frame->Navigate(TypeName(japanEasy::level::typeid), menuLocalSetting);
}
void japanEasy::menu::goToRecords_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
this->Frame->Navigate(TypeName(japanEasy::records::typeid), menuLocalSetting);
}
//on ajoute une fonction qui gère la back navigation
void menu::HardwareButtons_BackPressed(Object^ sender, Windows::Phone::UI::Input::BackPressedEventArgs^ e)
{
if (this->Frame->CanGoBack)
{
//On ferme l'application si l'utilisateur clique sur le bouton back du menu
Application::Current->Exit();
e->Handled = true;
}
}

Visual C++ fatal error LNK1120: 1 unresolved externals

Every time i try to run this program it gives me an error: EDITED
fatal error LNK1120: 1 unresolved externals
and another error
LNK2001: unresolved external symbol _main
EDITED
Ui.h:
#pragma once
namespace Spammer2 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Ui
/// </summary>
public ref class Ui : public System::Windows::Forms::Form
{
public:
Ui(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Ui()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ textBox1;
protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Timer^ timer1;
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(12, 13);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(260, 20);
this->textBox1->TabIndex = 0;
this->textBox1->TextChanged += gcnew System::EventHandler(this, &Ui::textBox1_TextChanged);
//
// button1
//
this->button1->Location = System::Drawing::Point(12, 138);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"Start";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Ui::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(196, 138);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 2;
this->button2->Text = L"Stop";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Ui::button2_Click);
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(172, 112);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(100, 20);
this->textBox2->TabIndex = 3;
this->textBox2->Text = L"100";
this->textBox2->TextChanged += gcnew System::EventHandler(this, &Ui::textBox2_TextChanged);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(62, 115);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(104, 13);
this->label1->TabIndex = 4;
this->label1->Text = L"Interval milliseconds:";
this->label1->Click += gcnew System::EventHandler(this, &Ui::label1_Click);
//
// timer1
//
this->timer1->Tick += gcnew System::EventHandler(this, &Ui::timer1_Tick);
//
// Ui
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 173);
this->Controls->Add(this->label1);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox1);
this->Name = L"Ui";
this->Text = L"Spammereme V2";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
timer1->Start();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
timer1->Stop();
}
private: System::Void textBox2_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
timer1->Interval = System::Int32::Parse(textBox2->Text);
SendKeys::Send(textBox1->Text);
SendKeys::Send("{ENTER}");
}
}
};
Ui.cpp:
#include "StdAfx.h"
#include "Ui.h"
The compiler error will say the line number for the question you originally asked.
However, it looks like you have
namespace Spammer2 {
//lots of usings
public ref class Ui : public System::Windows::Forms::Form
{
//lots of other stuff
}
};
You need to end the class definition with a semicolon, not the namespace:
namespace Spammer2 {
//lots of usings
public ref class Ui : public System::Windows::Forms::Form
{
//lots of other stuff
}; //<-------
} //<-------
Edit:
Since the question has now changed, if this is all your code add a main to UI.cpp
#include "StdAfx.h"
#include "Ui.h"
int main()
{
}

Qt inherit QSpinBox and QPushButton

I would like create my CustomQSpinBox .
This CustomQSpinBox must inherit of QPushButton and QSpinBox
but when I compile this code :
#include <QSpinBox>
#include <QPushButton>
class CustomQSpinBox : public QSpinBox, public QPushButton
{
Q_OBJECT
public:
CustomQSpinBox (QWidget *parent = 0);
~CustomQSpinBox ();
void initMinMax(int min, int max);
void init();
signals:
void needNumpad();
public slots:
void clicked();
};
I get an error :
erreur : C2594: 'static_cast'ÿ: conversions ambigu‰s de 'QObject *' en
'CustomQSpinBox *'
How I must do my inheritance ?
Yes, when the numPad is closed, the value is set in the QSpinBox. The problem is so to open the numPad when I click on the QSpinBox.
For the moment I manage with this code :
#include <QSpinBox>
#include <QPushButton>
#include <QMoveEvent>
#include <QResizeEvent>
class CustomQSpinBox: public QSpinBox
{
Q_OBJECT
public:
CustomQSpinBox(QWidget *parent = 0);
~CustomQSpinBox();
void resizeEvent(QResizeEvent *event);
void moveEvent(QMoveEvent * event);
signals:
void needNumpad();
public slots:
void clicked();
private:
QPushButton * button;
};

Form.cpp(16): error C2061: syntax error : identifier 'Form1'

Im trying to start working with Windows Forms... And I tryed to make programm by lesson... But it doesnt work and I dont understand why. If someone can help me it would be great.
My error:
1>ClCompile:
1> stdafx.cpp
1> AssemblyInfo.cpp
1> Form.cpp
1>Form.cpp(16): error C2872: 'Form1' : ambiguous symbol
1> could be 'Form1'
1> or 'c:\users\mizuru\documents\visual studio 2010\projects\form\form\Form1.h(15) : Form1::Form1'
1>Form.cpp(16): error C2061: syntax error : identifier 'Form1'
1> Generating Code...
1>
1>Build FAILED.
Form.cpp
// Form.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace Form1;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
Form1.h
#pragma once
namespace Form1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::TextBox^ textBox1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(113, 181);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(104, 13);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(35, 13);
this->label1->TabIndex = 1;
this->label1->Text = L"label1";
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(113, 110);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 20);
this->textBox1->TabIndex = 2;
this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
label1->Text="";
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
label1->Text=textBox1->Text;
}
};
}
The problem at hand is that your namespace and your form class both share the name Form1. You have to disambiguate the symbol by specifying that it's the class within the namespace.
Application::Run(gcnew ::Form1::Form1());

Showing the AppBar on right click on GridView item

In WinRT Applications, when you right click a ListBoxItem the AppBar is shown. But when you right click a GridViewItem the AppBar doesn't appear. Can this be configured?
If not is it beter to work with a ListBox instead of GridView and customize the templates. Or should I implement it With a RightTapped command.
(I work with MVVM Light, since Caliburn.Micro is currently not working)
Example of RightTappedCommand:
public sealed class RightTapped
{
#region Properties
#region Command
///
/// GetCommand
///
///
///
public static ICommand GetCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(CommandProperty);
}
///
/// SetCommand
///
///
///
public static void SetCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(CommandProperty, value);
}
///
/// DependencyProperty CommandProperty
///
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(RightTapped), new PropertyMetadata(null, OnCommandChanged));
#endregion Command
#region CommandParameter
///
/// GetCommandParameter
///
///
///
public static object GetCommandParameter(DependencyObject obj)
{
return (object)obj.GetValue(CommandParameterProperty);
}
///
/// SetCommandParameter
///
///
///
public static void SetCommandParameter(DependencyObject obj, object value)
{
obj.SetValue(CommandParameterProperty, value);
}
///
/// DependencyProperty CommandParameterProperty
///
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(RightTapped), new PropertyMetadata(null, OnCommandParameterChanged));
#endregion CommandParameter
#region HasCommandParameter
private static bool GetHasCommandParameter(DependencyObject obj)
{
return (bool)obj.GetValue(HasCommandParameterProperty);
}
private static void SetHasCommandParameter(DependencyObject obj, bool value)
{
obj.SetValue(HasCommandParameterProperty, value);
}
private static readonly DependencyProperty HasCommandParameterProperty =
DependencyProperty.RegisterAttached("HasCommandParameter", typeof(bool), typeof(RightTapped), new PropertyMetadata(false));
#endregion HasCommandParameter
#endregion Propreties
#region Event Handling
private static void OnCommandParameterChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
SetHasCommandParameter(o, true);
}
private static void OnCommandChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var element = o as FrameworkElement;
if (element != null)
{
if (e.NewValue == null)
{
element.RightTapped -= FrameworkElementKeyUp;
}
else if (e.OldValue == null)
{
element.RightTapped += FrameworkElementKeyUp;
}
}
}
private static void FrameworkElementKeyUp(object sender, RightTappedRoutedEventArgs e)
{
var o = sender as DependencyObject;
var command = GetCommand(sender as DependencyObject);
var element = e.OriginalSource as FrameworkElement;
if (element != null)
{
// If the command argument has been explicitly set (even to NULL)
if (GetHasCommandParameter(o))
{
var commandParameter = GetCommandParameter(o);
// Execute the command
if (command.CanExecute(commandParameter))
{
command.Execute(commandParameter);
}
}
else if (command.CanExecute(element.DataContext))
{
command.Execute(element.DataContext);
}
}
}
#endregion
}
In Xaml, something like this:
common:Tapped.Command="{Binding ShowAppBar}"
You can just do myAppBar.IsOpen = true.
It depends on the gridview selection mode, the right-click is used for selecting items. If you set the selectionmode property to None the appbar will open on right-click.