Set bindings for custom DependencyObjects - binding

This is a continuation of a question here: Trying to setup a custom DependencyObject. Clearly missing something. It's not practical to edit the original question; changes are too great. So I'm starting a fresh question.
I'm trying to setup bindings between custom DependencyObjects in my UWP app. The relevant code is below. I am seeing calls to ActualWidthPropertyChanged, but they are not triggering any call to WidthPropertyChanged. What am I missing?
class WindowsElement: DependencyObject
{
public WindowsElement()
{
}
public double Width
{
get
{
return (double)GetValue(WidthProperty);
}
set
{
SetValue(WidthProperty, value);
}
}
private static void WidthPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WindowsElement element = (WindowsElement)o;
double width = (double)e.NewValue;
CommonDebug.LogLine("WPC", element, o, width);
element.Width = width;
}
private static void ActualWidthPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WindowsElement element = (WindowsElement)o;
double width = (double)e.NewValue;
CommonDebug.LogLine("AWPC", o, e, width, element.Width);
element.ActualWidth = width;
}
public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(
"Width",
typeof(double),
typeof(WindowsElement),
new PropertyMetadata((double)0, WidthPropertyChanged));
public double ActualWidth {
get
{
return (double)GetValue(ActualWidthProperty);
}
set
{
SetValue(ActualWidthProperty, value);
}
}
public static readonly DependencyProperty ActualWidthProperty =
DependencyProperty.Register(
"ActualWidth",
typeof(double),
typeof(WindowsElement),
new PropertyMetadata((double)0, ActualWidthPropertyChanged));
public static void MessWithBindings()
{
WindowsElement we1 = new WindowsElement();
WindowsElement we2 = new WindowsElement();
var b = new Binding
{
Source = we2,
Path = new PropertyPath("ActualWidth")
};
BindingOperations.SetBinding(we1, WindowsElement.WidthProperty, b);
we2.ActualWidth = 13;
CommonDebug.LogLine(we1, we1.Width, we1.ActualWidth, we2, we2.Width, we2.ActualWidth);
}
}

I am seeing calls to ActualWidthPropertyChanged, but they are not triggering any call to WidthPropertyChanged. What am I missing?
To solve this question, you would need to implement the INotifyPropertyChanged interface on the source object so that the source can report changes.
Please see the following code:
class WindowsElement : DependencyObject, INotifyPropertyChanged
{
public WindowsElement()
{
}
public double Width
{
get
{
return (double)GetValue(WidthProperty);
}
set
{
SetValue(WidthProperty, value);
}
}
private static void WidthPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WindowsElement element = (WindowsElement)o;
double width = (double)e.NewValue;
CommonDebug.LogLine("WPC", element, o, width);
//element.Width = width;
element.RaisedPropertyChanged("Width");
}
private static void ActualWidthPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WindowsElement element = (WindowsElement)o;
double width = (double)e.NewValue;
CommonDebug.LogLine("AWPC", o, e, width, element.Width);
//element.ActualWidth = width;
element.RaisedPropertyChanged("ActualWidth");
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisedPropertyChanged(string PropertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(
"Width",
typeof(double),
typeof(WindowsElement),
new PropertyMetadata((double)0, WidthPropertyChanged));
public double ActualWidth
{
get
{
return (double)GetValue(ActualWidthProperty);
}
set
{
SetValue(ActualWidthProperty, value);
}
}
public static readonly DependencyProperty ActualWidthProperty = DependencyProperty.Register(
"ActualWidth",
typeof(double),
typeof(WindowsElement),
new PropertyMetadata((double)0, ActualWidthPropertyChanged));
public static void MessWithBindings()
{
WindowsElement we1 = new WindowsElement();
WindowsElement we2 = new WindowsElement();
var b = new Binding
{
Source = we2,
Path = new PropertyPath("ActualWidth")
};
BindingOperations.SetBinding(we1, WindowsElement.WidthProperty, b);
we2.ActualWidth = 13;
CommonDebug.LogLine(we1, we1.Width, we1.ActualWidth, we2, we2.Width, we2.ActualWidth);
}
}

Not sure why in UWP a one-way Binding from one dependency property to another doesn't automatically update the target property (as it does in WPF).
However, you could simply revert the direction of the Binding and make it two-way:
var b = new Binding
{
Source = we1,
Path = new PropertyPath("Width"),
Mode = BindingMode.TwoWay
};
BindingOperations.SetBinding(we2, WindowsElement.ActualWidthProperty, b);
we2.ActualWidth = 13;

Related

MonoGame/XNA Mouse Offsets

I am trying to use Mouse.GetState() for my menu selection. Currently, it will only highlight if I hover over a region left and up from where the menu is. I used DrawString to display the mouses coordinates and found that the 0,0 point wasn't in the top left of my monitor or in the top left of the game window. It was somewhere about 100,100 pixels from the top left of the screen. Also, the 0,0 point moves every time I run the programme.
I looked at others people who have had the same problem but wasn't able to solve it. I tried using Mouse.WindowHandle = this.Window.Handle; in my Initialize() but it didn't nothing. I have two monitors and when I forced the game in fullscreen it would open on my second monitor so I disabled it but the problem remains.
here is a link to my code http://pastebin.com/PNaFADqp
Game1 class:
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont spriteFont;
public const int WINDOW_HEIGHT = 800;
public const int WINDOW_WIDTH = 600;
public int tree;
public TitleScreen titleScreen;
public SATDemo satDemo;
public SeparatingAxisTest separatingAxisTest;
public SATWithAABB sATWithAABB;
GameState currentState;
public static Dictionary<string, Texture2D> m_textureLibrary = new Dictionary<string, Texture2D>();
public static Dictionary<string, SpriteFont> m_fontLibrary = new Dictionary<string, SpriteFont>();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
}
protected override void Initialize()
{
Mouse.WindowHandle = this.Window.Handle;
//enable the mousepointer
IsMouseVisible = true;
currentState = GameState.TitleScreen;
//sets the windows mouse handle to client bounds handle
base.Initialize();
}
public void RequestSATDemo()
{
currentState = GameState.RequestSATDemo;
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
m_textureLibrary.Add("Pixel", Content.Load<Texture2D>("White_Pixel"));
m_fontLibrary.Add("Font", Content.Load<SpriteFont>("MotorwerkOblique"));
titleScreen = new TitleScreen();
satDemo = new SATDemo();
separatingAxisTest = new SeparatingAxisTest();
sATWithAABB = new SATWithAABB();
}
public void RequestSeparatingAxisTest()
{
currentState = GameState.SeparatingAxisTest;
}
public void RequestSATWithAABB()
{
currentState = GameState.SATWithAABB;
}
protected override void Update(GameTime gameTime)
{
MouseTestState = Mouse.GetState();
switch (currentState)
{
case GameState.TitleScreen:
{
titleScreen.Update(gameTime);
break;
}
case GameState.SeparatingAxisTest:
{
separatingAxisTest.Update(gameTime);
break;
}
case GameState.SATWithAABB:
{
sATWithAABB.Update(gameTime);
break;
}
case GameState.Exit:
{
Exit();
break;
}
default:
{
titleScreen.Update(gameTime);
break;
}
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.DrawString(m_fontLibrary["Font"], MouseTestState.ToString(), new Vector2(0, 0), Color.White);
switch (currentState)
{
case GameState.TitleScreen:
{
titleScreen.Draw(spriteBatch, spriteFont);
break;
}
case GameState.SeparatingAxisTest:
{
separatingAxisTest.Draw(gameTime, spriteBatch);
break;
}
case GameState.SATWithAABB:
{
sATWithAABB.Draw(gameTime, spriteBatch);
break;
}
case GameState.Exit:
{
Exit();
break;
}
default:
{
titleScreen.Update(gameTime);
break;
}
}
spriteBatch.End();
base.Draw(gameTime);
}
}
TitleScreen class:
public class TitleScreen : Screen
{
List<Button> buttonList = new List<Button>();
public Menu mainMenu;
public TitleScreen()
{
mainMenu = new Menu(new Vector2(200, 100), buttonList, 0);
buttonList.Add(new PushButton("Separating Axis Test"));
buttonList.Add(new PushButton("SAT With AABB"));
buttonList.Add(new PushButton("Awesome"));
buttonList.Add(new PushButton("Awesomere"));
buttonList.Add(new PushButton("Awesomere"));
}
public override void Update(GameTime gametime)
{
mainMenu.Update(gametime);
}
public void Draw(SpriteBatch sB, SpriteFont sF)
{
mainMenu.Draw(sB, sF);
}
}
PushButton class:
public class PushButton : Button
{
string m_text;
SpriteFont m_font;
Color m_static, m_onClick, m_onHover;
Texture2D m_sprite2D, m_onClick2D;
static public int Pbuttoncount;
//click processing
bool m_clickedInside = false,
m_releasedInside = false,
m_OnClicked = false,
selected = false;
Rectangle drawRectangle;
public PushButton(string Text)
{
m_text = Text;
drawRectangle = new Rectangle((int)Menu.m_position.X, (int)Menu.m_position.Y + (15 * Pbuttoncount), 200, 15);
ButtonRegion = new Rectangle((int)Position.X, (int)Position.Y, 200, 15);
Pbuttoncount++;
}
public PushButton(Rectangle ButtonRegion, SpriteFont Font, string Text, Color Static, Color OnClick, Color OnHover)
{
m_buttonRegion = ButtonRegion;
m_font = Font;
m_text = Text;
m_static = Static;
m_onClick = OnClick;
m_onHover = OnHover;
// drawRectangle = ButtonPosition(m_buttonRegion);
}
public PushButton(Rectangle ButtonRegion, Texture2D Sprite2D, Texture2D OnClick2D)
{
m_buttonRegion = ButtonRegion;
m_sprite2D = Sprite2D;
m_onClick2D = OnClick2D;
//drawRectangle = ButtonPosition(m_buttonRegion);
}
public override void Update(GameTime gameTime)
{
MouseState currentMouse = Mouse.GetState();
selected = MouseState(drawRectangle, currentMouse);
m_clickedInside = ClickInside(currentMouse, m_lastMouseState);
ReleaseInside(currentMouse, m_lastMouseState);
if (selected && m_clickedInside && m_releasedInside)
m_OnClicked = true;
else
m_OnClicked = false;
m_lastMouseState = currentMouse;
}
public override void Draw(SpriteBatch spriteBatch, SpriteFont spriteFont, int buttonCount, Vector2 Position)
{
spriteBatch.Draw(Game1.m_textureLibrary["Pixel"], new Rectangle((int)Position.X + 10, (int)(Position.Y + 15 * buttonCount), 180, 15), Color.Wheat);
if (selected)
spriteBatch.DrawString(Game1.m_fontLibrary["Font"], m_text, new Vector2(Position.X + 15, Position.Y + 15 * buttonCount), Color.Orange);
else
spriteBatch.DrawString(Game1.m_fontLibrary["Font"], m_text, new Vector2(Position.X + 15, Position.Y + 15 * buttonCount), Color.Black);
}
}
Menu class:
public class Menu
{
List<Button> m_buttonList;
float m_transparency;
public int n = 0;
public Rectangle buttonRegion, m_menuRegion, m_dimensions;
static public Vector2 m_position;
int m_WINDOW_HEIGHT = Game1.WINDOW_HEIGHT;
int m_WINDOW_WIDTH = Game1.WINDOW_WIDTH;
private Game1 m_managerClass;
public Menu(Vector2 Position, List<Button> ButtonList, float Transparency)
{
m_position = Position;
m_buttonList = ButtonList;
m_transparency = Transparency;
m_managerClass = new Game1();
}
public Rectangle MenuRegion
{
get { return m_menuRegion; }
set { m_menuRegion = value; }
}
static public Vector2 Position
{
get { return m_position; }
}
public void Update(GameTime gametime)
{
for (int i = 0; i < m_buttonList.Count; i++)
{
m_buttonList[i].Update(gametime);
if (m_buttonList[0].OnClicked)
{
SeperatingAxisTest();
}
}
}
public void Draw(SpriteBatch sB, SpriteFont sF)
{
sB.Draw(Game1.m_textureLibrary["Pixel"], new Rectangle((int)m_position.X - 5, (int)m_position.Y - 10, (m_buttonList[0].ButtonRegion.Width + 10), (m_buttonList[0].ButtonRegion.Height * m_buttonList.Count) + 20), Color.Blue);
for (int i = 0; i < m_buttonList.Count; i++)
{
m_buttonList[i].Draw(sB, sF, i, new Vector2(Position.X, Position.Y));
}
}
private void SeperatingAxisTest()
{
m_managerClass.RequestSeparatingAxisTest();
}
}
Program class:
public static class Program
{
[STAThread]
static void Main()
{
using (var game = new Game1())
game.Run();
}
}
Let me know if you need anything else. I'm still learning and will sell my soul to you for an answer.
Your Menu class is creating a new instance of Game1. This is, most likely, not what you want, since Game1 is instantiated in the entry point for you app. The Game1 instance has an instance of TitleScreen, which in turn has an instance of the Menu class, so a Menu should have no business creating its own game.
When this (other) instance is created, it invokes platform-specific (Windows) methods, creates an additional window handle (which is never shown) and configures the Mouse.WindowHandle.
And btw, setting WindowHandle manually does absolutely nothing in Monogame, so all these sources mentioning that are talking about XNA.
So, there are several remarks:
You should probably have a "screen manager" class which contains the current screen. It is strange to have a field of type TitleScreen in your game class, it should at least be of the base type (Screen), so that the game class draws and updates each screen transparently.
If you need a reference to the game class anywhere, don't instantiate a new one, but rather pass it along through the constructor.
m_managerClass is a bad name for a field which is actually a Game. Also google for C# naming conventions. Perhaps you even might want to download an existing monogame game template, e.g. check some of the samples online; the NetRumble sample seems to implement a screen manager.
Remove the Mouse.WindowHandle line, it should be set to your one-and-only game window by default.
tl;dr add the Game1 as a parameter wherever you might need it (but only where you need it).
abstract class Screen
{
private readonly Game1 _game;
public Game1 Game
{ get { return _game; } }
public Screen(Game1 game)
{
_game = game;
}
}
class TitleScreen : Screen
{
public TitleScreen(Game1 game)
: base(game)
{ ... }
}
class Menu
{
private readonly Screen _screen;
public Menu(Screen parentScreen, Vector2 pos, List<Button> list, float alpha)
{
_screen = parentScreen;
...
// if you need the game instance, just use _screen.Game
}
}

Xamarin Android Custom View Not Loading

i am new to Xamarin monoandroid and i am facing a problem in adding a custom view to my layout. i have no idea what i am doing wrong. little help will be great full and this is my custom view class
onDraw method is called but i don't get the output and no errors.
class PositionDrawer : View
{
private int count;
private int position;
private IPositionDrawer cinterface;
private Activity cactivity;
private int container;
private int p;
private ScoreFeed scoreFeed1;
private ScoreFeed scoreFeed2;
public PositionDrawer(Activity context, int count, int positionr, Activity cactivity) :
base (context)
{
//Initialize ();
this.cactivity = cactivity;
this.cinterface = cinterface;
this.count = count;
this.position = positionr;
//this.OnDraw();
Initialize();
}
private void Initialize()
{
//this.Invalidate();
// this.SetMeasuredDimension(100, 100);
this.SetWillNotDraw(false);
Console.WriteLine(this.WillNotDraw());
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void SetDrawer(int count, int positionr, Activity cactivity)
{
this.cactivity = cactivity;
this.cinterface = cinterface;
this.count = count;
this.position = positionr;
//base(cactivity);
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
Paint blue = new Paint();
blue.Color = Color.Red;
float screenwidth = cactivity.WindowManager.DefaultDisplay.Width;
canvas.DrawRect(screenwidth * position-1, 0, screenwidth * position, 20, blue);
Console.WriteLine("SW1:" + screenwidth + "pos1:" + position);
// LinearLayout can =cactivity.FindViewById<LinearLayout>(container);
//cinterface.getview(this);
}
}
and i use following code to add view to the layout.
View pd = new PositionDrawer(this, 3, position, this);
container = FindViewById<LinearLayout>(Resource.Id.position);
container.RemoveAllViews();
container.AddView(pd);
thanks in advance.
You need to implement the correct ctors.
When you implement the View class then you have to at least have the ctor with the following signature:
public PositionDrawer(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer) { }
And if you are using the View in a AXML layout you also need the one with the following signature:
public PositionDrawer(Context context, IAttributeSet attrs, int defStyle)
: base(context, attrs, defStyle)
Otherwise when you create Views from code, you need to give them some LayoutParameters otherwise they do not know their own size.

Custom container not drawing

I've been reading about Vala over the past couple of days and decided to dive into it and make some Clutter widgets along the way. I'm currently trying to draw a private actor from my custom actor subclass. Here is a simplified version of what I've got so far.
public class MyContainer : Clutter.Actor, Clutter.Container {
private Clutter.Group group;
public MyContainer() {
group = new Clutter.Group();
group.set_parent(this);
}
public void add_actor(Clutter.Actor actor) {
group.add_actor(actor);
actor.show();
set_size(group.width, group.height);
actor_added(actor);
queue_redraw();
}
public void foreach(Clutter.Callback callback) {
group.foreach(callback);
queue_redraw();
}
public override void get_preferred_height(
float for_width,
out float min_height_p,
out float natural_height_p) {
group.get_preferred_height(
for_width,
out min_height_p,
out natural_height_p);
}
public override void get_preferred_width(
float for_height,
out float min_width_p,
out float natural_width_p) {
group.get_preferred_width(
for_height,
out min_width_p,
out natural_width_p);
}
public override void paint() {
group.paint();
}
public void remove_actor(Clutter.Actor actor) {
group.remove_actor(actor);
set_size(group.width, group.height);
actor_removed(actor);
queue_redraw();
}
public void sort_depth_order() {
group.sort_depth_order();
queue_redraw();
}
}
int main(string [] args) {
// Start clutter.
var result = Clutter.init(ref args);
if (result != Clutter.InitError.SUCCESS) {
stderr.printf("Error: %s\n", result.to_string());
return 1;
}
var stage = Clutter.Stage.get_default();
// Build a MyCollection object.
var myc = new MyContainer();
myc.x = 100;
myc.y = 100;
var r1 = new Clutter.Rectangle();
r1.width = 50;
r1.height = 50;
r1.color = Clutter.Color.from_string("rgb(255, 0, 0)");
var t1 = new Clutter.Text();
t1.text = "The red square.";
t1.y = r1.height;
// Build a Group object similar to the previous.
var group = new Clutter.Group();
group.x = 300;
group.y = 100;
var r2 = new Clutter.Rectangle();
r2.width = 50;
r2.height = 50;
r2.color = Clutter.Color.from_string("rgb(255, 0, 0)");
var t2 = new Clutter.Text();
t2.text = "The red square.";
t2.y = r2.height;
// Display.
myc.add_actor(r1);
myc.add_actor(t1);
group.add_actor(r2);
group.add_actor(t2);
stage.add_actor(myc);
stage.add_actor(group);
stage.show_all();
Clutter.main();
return 0;
}
The example paints the group added directly to the stage, but not the group wrapped by the custom collection that is added to the stage. How can I get this to work and what is wrong with the above?
I've been working on ubuntu 11.10 with valac --pkg clutter-1.0 above_code_example.vala.
This answer is from buz on gnome.irc's #clutter room.
The problem is a missing override for the allocate function.

Backup/Restore net.rim.device.api.util.Persitable object with DesktopManger

The task is to backup/restore Persistable object with BB Desktop Manager or in any other way. The main aim is to keep data between device firmware updates...
I have:
public final class UserList implements Persistable {
//The persistable objects.
private Hashtable fData;
//Initialize the class with empty values.
public UserList() {
fData = new Hashtable();
}
//Initialize the class with the specified values.
public UserList(Hashtable p) {
fData = p;
}
public Hashtable getData() {
return fData;
}}
I also have implemented SyncItem (as found in one of the examples)
public final class UserListSync extends SyncItem {
private static UserList fList;
private static final int FIELDTAG_NAME = 1;
private static final int FIELDTAG_AGE = 2;
private static PersistentObject store;
static {
store = PersistentStore.getPersistentObject(0x3167239af4aa40fL);
}
public UserListSync() {
}
public String getSyncName() {
return "Sync Item Sample";
}
public String getSyncName(Locale locale) {
return null;
}
public int getSyncVersion() {
return 1;
}
public boolean getSyncData(DataBuffer db, int version) {
boolean retVal = true;
synchronized (store) {
if (store.getContents() != null) {
fList = (UserList)store.getContents();
}
}
try {
Enumeration e = fList.getData().keys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = (String) fList.getData().get(key);
//Write the name.
db.writeShort(key.length() + 1);
db.writeByte(FIELDTAG_NAME);
db.write(key.getBytes());
db.writeByte(0);
//Write the age.
db.writeShort(value.length() + 1);
db.writeByte(FIELDTAG_AGE);
db.write(value.getBytes());
db.writeByte(0);
}
} catch (Exception e) {
retVal = false;
}
return retVal;
}
//Interprets and stores the data sent from the Desktop Manager.
public boolean setSyncData(DataBuffer db, int version) {
int length;
Hashtable table = new Hashtable();
Vector keys = new Vector();
Vector values = new Vector();
boolean retVal = true;
try {
//Read until the end of the Databuffer.
while (db.available() > 0) {
//Read the length of the data.
length = db.readShort();
//Set the byte array to the length of the data.
byte[] bytes = new byte[length];
//Determine the type of data to be read (name or age).
switch (db.readByte()) {
case FIELDTAG_NAME:
db.readFully(bytes);
keys.addElement(new String(bytes).trim());
break;
case FIELDTAG_AGE:
db.readFully(bytes);
values.addElement(new String(bytes).trim());
break;
}
}
} catch (Exception e) {
retVal = false;
}
for (int i = 0; i < keys.size(); i++) {
table.put(keys.elementAt(i), values.elementAt(i));
}
try {
//Store the new data in the persistent store object.
fList = new UserList(table);
store.setContents(fList);
store.commit();
} catch (Exception e) {
retVal = false;
}
return retVal;
}}
The entry poing is following:
public class SyncItemSample extends UiApplication {
private static PersistentObject store;
private static UserList userList;
static {
store = PersistentStore.getPersistentObject(0x3167239af4aa40fL);
}
public static void main(String[] args) {
SyncItemSample app = new SyncItemSample();
app.enterEventDispatcher();
}
public SyncItemSample() {
UserListScreen userListScreen;
//Check to see if the store exists on the BlackBerry.
synchronized (store) {
if (store.getContents() == null) {
//Store does not exist, create it with default values
userList = new UserList();
store.setContents(userList);
store.commit();
} else {
//Store exists, retrieve data from store.
userList = (UserList)store.getContents();
}
}
//Create and push the UserListScreen.
userListScreen = new UserListScreen(userList);
pushScreen(userListScreen);
}}
And here is an implementation of screen:
public final class UserListScreen extends MainScreen {
Vector fLabels = new Vector();
Vector fValues = new Vector();
VerticalFieldManager leftColumn = new VerticalFieldManager();
VerticalFieldManager rightColumn = new VerticalFieldManager();
UserList fList;
public UserListScreen(UserList list) {
super();
fList = list;
//Create a horizontal field manager to hold the two vertical field
//managers to display the names and ages in two columns.
VerticalFieldManager inputManager = new VerticalFieldManager();
HorizontalFieldManager backGround = new HorizontalFieldManager();
//Array of fields to display the names and ages.
LabelField title = new LabelField("User List",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
final TextField fld1 = new TextField(TextField.NO_NEWLINE);
fld1.setLabel("input label");
inputManager.add(fld1);
final TextField fld2 = new TextField(TextField.NO_NEWLINE);
fld2.setLabel("input value");
inputManager.add(fld2);
final ButtonField fld3 = new ButtonField();
fld3.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
fList.getData().put(fld1.getText().trim(), fld2.getText().trim());
refresh();
}
});
fld3.setLabel("add");
inputManager.add(fld3);
add(inputManager);
//Add the column titles and a blank field to create a space.
LabelField leftTitle = new LabelField("label ");
leftColumn.add(leftTitle);
LabelField rightTitle = new LabelField("value");
rightColumn.add(rightTitle);
refresh();
//Add the two vertical columns to the horizontal field manager.
backGround.add(leftColumn);
backGround.add(rightColumn);
//Add the horizontal field manager to the screen.
add(backGround);
}
private void refresh() {
leftColumn.deleteAll();
rightColumn.deleteAll();
fLabels.removeAllElements();
fValues.removeAllElements();
//Populate and add the name and age fields.
Enumeration e = fList.getData().keys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = (String) fList.getData().get(key);
final LabelField tmp1 = new LabelField(key);
final LabelField tmp2 = new LabelField(value);
leftColumn.add(tmp1);
rightColumn.add(tmp2);
fLabels.addElement(tmp1);
fValues.addElement(tmp2);
}
}
public boolean onClose() {
System.exit(0);
return true;
}}
So as you see it should be very easy...
So all of these I run application, add values to Persistent object and they are added correctly, are stored during device resets and so on...
When I run Desktop Manager and make a Backup it seems that UserList is backed-up, as size of backup grows together with adding new data into persistent store.
But when I run "Wipe device" on my BB 9300 (and all data from Persistent store is cleared as it is expected) and then run Restore from just made backup file - nothing is updated in the Application and persistent store is seems to be empty.
In some examples I have found adding alternate entry point "init" but I can't tune eveything like it is described with my EclipsePlugin
Could you advice me how to store data in backup file and the to retrieve the same data from backup and load it back to the application, or how to log any of events with Desktop Manager?
If someone has experienced the same problem you can try to disconnect the device before wiping it. It is strange but it helped :)

Cleanest single click + double click handling in Silverlight?

I've been finding various methods of dealing with double click and then the authors slap on some if code for handling single clicks. Is there a standard now in Silverlight 3 that everyone is using to handle both a single and a double click on listboxes?
If you use the Reactive Extensions (Rx) library the code to support double click is much simpler:
Observable.FromEvent<MouseButtonEventArgs>(myControl, "MouseLeftButtonDown").TimeInterval().Subscribe(evt =>
{
if (evt.Interval.TotalMilliseconds <= 300)
{
// Do something on double click
}
});
Write once use easily....
import YourProject.Utils; //must for using extentions
button1.AddDoubleClickHandler((s, e) =>
{
Debug.WriteLine("You can use this DoubleClick extention method
for any UIElement in SL !");
});
//Here is my util
namespace YourProject.Utils
{
public class DoubleClick
{
public DoubleClick()
{
this._lastClick = DateTime.Now;
}
private TimeSpan DoubleClickThreshold = TimeSpan.FromMilliseconds(450);
private DateTime _lastClick;
public event MouseButtonEventHandler MouseDoubleClick;
public void DoubleClicked(object sender, MouseButtonEventArgs e)
{
if (DateTime.Now - this._lastClick <= DoubleClickThreshold)
{
MouseDoubleClick(sender, e);
}
this._lastClick = DateTime.Now;
}
internal void AddHandler(UIElement ctl)
{
ctl.AddHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(this.DoubleClicked), true);
}
}
public static class DoubleClickExtentions
{
public static void AddDoubleClickHandler(this UIElement ctl, MouseButtonEventHandler MouseDoubleClick)
{
DoubleClick doubleClick = new DoubleClick();
doubleClick.MouseDoubleClick += MouseDoubleClick;
doubleClick.AddHandler(ctl);
}
}
}
I like this approach:
http://www.domagoj.pavlesic.com/DoubleClick-in-Silverlight
Pros: you have single click, double click and single click delayed events (so you can be sure, there will be no double click).
I've implemented a clean way to register for DoubleClick events based on the approaches of the following articles:
http://yinyangme.com/blog/post/The-simplest-way-to-detect-DoubleClick-in-Silverlight.aspx
http://www.domagoj.pavlesic.com/DoubleClick-in-Silverlight
To use it, you just need to register/unregister the handler through extension methods:
element.AddDoubleClickHandler(Element_DoubleClick);
element.RemoveDoubleClickHandler(Element_DoubleClick);
Here is the code:
using System;
using System.Windows;
using System.Windows.Input;
namespace System.Windows
{
public class DoubleClickHelper
{
private const long DoubleClickSpeed = 500;
private const double MaxMoveDistance = 10;
private static long lastClickTicks = 0;
private static Point lastPosition;
private static WeakReference lastSender;
internal static bool IsDoubleClick(object sender, MouseButtonEventArgs e)
{
Point position = e.GetPosition(null);
long clickTicks = DateTime.Now.Ticks;
long elapsedTicks = clickTicks - lastClickTicks;
long elapsedTime = elapsedTicks / TimeSpan.TicksPerMillisecond;
bool quickClick = (elapsedTime <= DoubleClickSpeed);
bool senderMatch = (lastSender != null && sender.Equals(lastSender.Target));
if (senderMatch && quickClick && DoubleClickHelper.Distance(position, lastPosition) <= MaxMoveDistance)
{
// Double click!
lastClickTicks = 0;
lastSender = null;
return true;
}
// Not a double click
lastClickTicks = clickTicks;
lastPosition = position;
if (!quickClick)
lastSender = new WeakReference(sender);
return false;
}
private static double Distance(Point pointA, Point pointB)
{
double x = pointA.X - pointB.X;
double y = pointA.Y - pointB.Y;
return Math.Sqrt(x * x + y * y);
}
public bool HasHandlers { get { return this.MouseDoubleClick != null; } }
private WeakReference target;
public event MouseButtonEventHandler MouseDoubleClick;
private void OnMouseDoubleClick(MouseButtonEventArgs args)
{
if (this.MouseDoubleClick != null && this.target.IsAlive)
this.MouseDoubleClick(this.target.Target, args);
}
public DoubleClickHelper(FrameworkElement target)
{
this.target = new WeakReference(target);
target.MouseLeftButtonDown += target_MouseLeftButtonDown;
}
void target_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (DoubleClickHelper.IsDoubleClick(sender, e))
this.OnMouseDoubleClick(e);
}
}
public static class DoubleClickExtension
{
public static DoubleClickHelper GetDoubleClickHelper(DependencyObject obj)
{
return (DoubleClickHelper)obj.GetValue(DoubleClickHelperProperty);
}
public static void SetDoubleClickHelper(DependencyObject obj, DoubleClickHelper value)
{
obj.SetValue(DoubleClickHelperProperty, value);
}
public static readonly DependencyProperty DoubleClickHelperProperty =
DependencyProperty.RegisterAttached("DoubleClickHelper", typeof(DoubleClickHelper), typeof(DoubleClickExtension), new PropertyMetadata(null));
public static void AddDoubleClickHandler(this FrameworkElement target, MouseButtonEventHandler handler)
{
DoubleClickHelper helper = target.GetValue(DoubleClickHelperProperty) as DoubleClickHelper;
if (helper == null)
{
helper = new DoubleClickHelper(target);
target.SetValue(DoubleClickHelperProperty, helper);
}
helper.MouseDoubleClick += handler;
}
public static void RemoveDoubleClickHandler(this FrameworkElement target, MouseButtonEventHandler handler)
{
DoubleClickHelper helper = target.GetValue(DoubleClickHelperProperty) as DoubleClickHelper;
if (helper == null) return;
helper.MouseDoubleClick -= handler;
if(!helper.HasHandlers)
target.SetValue(DoubleClickHelperProperty, null);
}
}
}
There is an easier way in Silverlight 5, which supports MouseButtonEventArgs.ClickCount. So, you can just attach a normal MouseLeftButtonDown handler, and check:
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs args)
{
if (args.ClickCount == 1)
return;
// handle double-click
}
Here's a class I've implemented for controls and also a second derived class below for a treeview (Silverlight Toolkit). Just instantiate it with the control you want to check for double clicks and add a handler for the DoubleClicked event. It uses a timer to try to simulate a double-click event. You can change the delay if you think it will work better.
Public Class DoubleClickHelper
Public Event DoubleClicked(ByVal sender As FrameworkElement)
Private WithEvents UI As FrameworkElement
Sub New(ByRef UI As FrameworkElement)
Me.UI = UI
UI.AddHandler(UIElement.MouseLeftButtonDownEvent, New MouseButtonEventHandler(AddressOf UI_MouseLeftButtonDown), True)
InitTimer()
End Sub
Public Delay As Single = 0.2
Private _dblclick As Boolean = False
Private _timer As New System.Windows.Threading.DispatcherTimer()
Protected Property DoubleClick() As Boolean
Get
Return _dblclick
End Get
Set(ByVal value As Boolean)
_dblclick = value
InitTimer()
End Set
End Property
Private Sub InitTimer()
RemoveHandler _timer.Tick, AddressOf timer_Tick
_timer.Stop()
_timer = New System.Windows.Threading.DispatcherTimer()
_timer.Interval = TimeSpan.FromSeconds(Delay)
AddHandler _timer.Tick, AddressOf timer_Tick
_timer.Start()
End Sub
Protected Overridable Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
DoubleClick = False
End Sub
Protected Overridable Sub UI_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles UI.MouseLeftButtonDown
If DoubleClick Then
HandleDoubleClick(sender)
Else
HandleFirstClick(sender)
End If
End Sub
Protected Overridable Sub HandleDoubleClick(ByVal sender As FrameworkElement)
RaiseEvent DoubleClicked(sender)
End Sub
Protected Overridable Sub HandleFirstClick(ByVal sender As FrameworkElement)
DoubleClick = True
End Sub
End Class
Public Class TreeViewItemDoubleClickHelper
Inherits DoubleClickHelper
Private SameSelection As Boolean = False
Private WithEvents TreeView As TreeView = Nothing
Public Sub New(ByVal TreeView As TreeView)
MyBase.New(TreeView)
Me.TreeView = TreeView
End Sub
'This event happens after MouseLeftButtonDown
Private Sub TreeView_SelectedItemChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles TreeView.SelectedItemChanged
SameSelection = e.OldValue Is e.NewValue
End Sub
Protected Overrides Sub UI_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
'MyBase.UI_MouseLeftButtonDown(sender, e)
If DoubleClick Or SameSelection Then
HandleDoubleClick(sender)
SameSelection = False
DoubleClick = False
Else
HandleFirstClick(sender)
End If
End Sub
End Class

Resources