I'm taking over a project on Xamarin.
It work on android and I want it to work on iOS.
Initially, it have been done for that so I expected it work with maybe only settings ?
Anyway it work on Android but not on iOS.
I have this code :
public App()
{
Debug.WriteLine(Current.Properties); //return default value
try
{
ViewModelLocator.MainViewModel.RestoreState(Current.Properties);
BindingContext = ViewModelLocator.MainViewModel;
MainTabPage = new SGC400Tab();
MainPage = MainTabPage;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
public static class ViewModelLocator
{
private static GlobalViewModel _myViewModel = new GlobalViewModel();
public static GlobalViewModel MainViewModel
{
get
{
return _myViewModel;
}
}
}
public class GlobalViewModel : INotifyPropertyChanged
{
public void RestoreState(IDictionary<string, object> dictionary)
{
Thing1.SelectedUnitySystem_Presure = GetDictionaryEntry(dictionary, "SavedUnitySystem_Pression", "Bars");
Thing1.SelectedUnitySystem_Flow = GetDictionaryEntry(dictionary, "SavedUnitySystem_Debit", "m3/h");
Thing1.SelectedUnitySystem_Temperature = GetDictionaryEntry(dictionary, "SavedUnitySystem_Temperature", "°C");
Thing1.SelectedLanguageKey = GetDictionaryEntry(dictionary, "SavedLanguage", "en");
}
}
Which return an error :
System.NullReferenceException on line :
private static GlobalViewModel _myViewModel = new GlobalViewModel();
I'm a bit puzzled by the fact it work on android... but not on iOS...
I have to tell I'm pretty new on Xamarin, may you give me some pointer ?
Thank you in advance !
PS : Class GlobalViewModel here :
public class GlobalViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
//#####################################
//Enneigeur
public Enneigeur _Enneigeur1 = new Enneigeur();
public Enneigeur Enneigeur1
{
get { return _Enneigeur1; }
set
{
_Enneigeur1 = value;
}
}
//Com
public ComSGC400 _comSGC400 = new ComSGC400();
public ComSGC400 ComSGC400
{
get { return _comSGC400; }
set { _comSGC400 = value; }
}
public const int TIMOUT_COM_MS = 500;
public const int DELAI_ENTRE_TRAME_MS = 1000;
//#####################################
//Bluetooth
public ObservableCollection<string> ListOfDevices { get; set; } = new ObservableCollection<string>();
public string inputBuffer;
public string SelectedBthDevice { get;set; } = "";
private bool _isConnected { get; set; } = false;
public bool IsConnected {
get { return _isConnected; }
set
{
_isConnected = value;
if (_isConnected == true)
{
((App)Application.Current).AddPages();
} else
{
((App)Application.Current).RemovePages();
}
}
}
private bool _isSelectedBthDevice { get { if (string.IsNullOrEmpty(SelectedBthDevice)) return false; return true; }}
public bool IsConnectEnabled { get { if (_isSelectedBthDevice == false) return false; return !IsConnected; }}
public bool IsDisconnectEnabled { get { if (_isSelectedBthDevice == false) return false; return IsConnected; } }
public Color ConnectBackgroundcolor { get { if (IsConnectEnabled) return Color.Green; return Color.FromRgb(48, 48, 48); } }
public Color DisconnectBackgroundColor { get { if (IsDisconnectEnabled) return Color.Red; return Color.FromRgb(48, 48, 48); } }
public bool IsConnectionInit { get { if (EtatGrafcet_loop < 2 || IsConnected == false ) return false; return true; } }
public bool IsModeManuOK { get { if (EtatGrafcet_loop >= 2 && IsConnected == true && _Enneigeur1.Manu == true) return true; return false; } }
public bool IsBoutonRotaOK { get { if (EtatGrafcet_loop >= 2 && IsConnected == true && ( _Enneigeur1._VersionSGC400 > 10213 || _Enneigeur1.Manu == true)) return true; return false; } }
public bool IsPickerEnabled { get { return !IsConnected; }}
public bool IsBalayageDispo { get { if (EtatGrafcet_loop < 2 || IsConnected == false || _Enneigeur1.OptionBalayageDispo == false) return false; return true; } }
public LocalizedResources Resources { get; private set;}
public Color Mode_Manu_Button_Text_Color
{
get
{
if (EtatGrafcet_loop < 2)
return Color.Gray;
else
return Color.White;
}
}
public Color Mode_Manu_Button_Color {
get {
if (EtatGrafcet_loop < 2)
return Color.FromRgb(48, 48, 48);
else
{
if (_Enneigeur1.ModeMarche == 1) return Color.Green; return Color.Black;
}
}
}
public Color Mode_Stop_Button_Text_Color
{
get
{
if (EtatGrafcet_loop < 2)
return Color.Gray;
else
return Color.White;
}
}
public Color Mode_Stop_Button_Color {
get
{
if (EtatGrafcet_loop < 2)
return Color.FromRgb(48, 48, 48);
else
{
if (_Enneigeur1.ModeMarche == 2) return Color.Green; return Color.Black;
}
}
}
public Color Mode_Forcage_Button_Text_Color
{
get
{
if (EtatGrafcet_loop < 2)
return Color.Gray;
else
return Color.White;
}
}
public Color Mode_Forcage_Button_Color
{
get
{
if (EtatGrafcet_loop < 2)
return Color.FromRgb(48, 48, 48);
else
{
if (_Enneigeur1.ModeMarche == 0) return Color.Green; return Color.Black;
}
}
}
private CancellationTokenSource _ct { get; set; }
private int EtatGrafcet_loop { get; set; }
private DateTime timer_send = DateTime.Now;
private bool envoiContinu = false;
private string envoiWrite = "";
private async Task Loop()
{
string bufferEnvoi = "";
bool res;
int erreur_cryptage = 0;
EtatGrafcet_loop = 0;
int reponseLen = 0;
_Enneigeur1.ResetValues();
_ct = new CancellationTokenSource();
ComSGC400.AddrSGC400 = Convert.ToUInt16(SelectedBthDevice.Substring(5));
while (_ct.IsCancellationRequested == false)
{
switch (EtatGrafcet_loop)
{
case 0://Envoi trame clé crypté
if (DateTime.Now > timer_send.AddMilliseconds(DELAI_ENTRE_TRAME_MS)) //Temps entre trames
{
//Test d'echange de clé de cryptage
inputBuffer = "";
reponseLen = ComSGC400.Create_Trame_getKey(ref bufferEnvoi);
if (bufferEnvoi.Length > 0)
{
Xamarin.Forms.MessagingCenter.Send<App, string>((App)Xamarin.Forms.Application.Current, "WriteDatas", bufferEnvoi);
timer_send = DateTime.Now;
EtatGrafcet_loop = 1;
}
}
break;
case 1: //attente reception clé crypté
if (DateTime.Now > timer_send.AddMilliseconds(TIMOUT_COM_MS)){ EtatGrafcet_loop = 0; break; } //timeout
if (inputBuffer != null)
{
ComSGC400.Trame_RechercheDebutTrame(ref inputBuffer);
if (inputBuffer.Length >= reponseLen)
{
inputBuffer = inputBuffer.Substring(0, reponseLen);
res = ComSGC400.Update_ProtectionKey( inputBuffer);
if (res == true)
{
//cle cryptage OK
bufferEnvoi = "";
EtatGrafcet_loop = 2;
erreur_cryptage = 0;
}
else
{
//cle cryptage non ok
bufferEnvoi = "";
EtatGrafcet_loop = 0;
}
inputBuffer = "";
}
}
break;
case 2: //envoi trame readMain
if (envoiWrite != "")
EtatGrafcet_loop = 10; //envoi write
else
{
if (DateTime.Now > timer_send.AddMilliseconds(DELAI_ENTRE_TRAME_MS)) //Temps entre trames
{
reponseLen = ComSGC400.Create_Trame_ReadMain(ref bufferEnvoi);
if (bufferEnvoi.Length > 0)
{
inputBuffer = "";
Xamarin.Forms.MessagingCenter.Send<App, string>((App)Xamarin.Forms.Application.Current, "WriteDatas", bufferEnvoi);
timer_send = DateTime.Now;
EtatGrafcet_loop = 3;
}
}
}
break;
case 3: //attente reponse trame readMain
if (DateTime.Now > timer_send.AddMilliseconds(TIMOUT_COM_MS)) {
EtatGrafcet_loop = 20; break;
} //timeout
if (inputBuffer != null)
{
ComSGC400.Trame_RechercheDebutTrame(ref inputBuffer);
if (inputBuffer.Length >= reponseLen)
{
inputBuffer = inputBuffer.Substring(0, reponseLen);
res = ComSGC400.Update_Enneigeur( inputBuffer, ref _Enneigeur1);
if (res == true)
{
//Message OK
bufferEnvoi = "";
EtatGrafcet_loop = 4;//Passage readCom
erreur_cryptage = 0;
_Enneigeur1.LastRead = DateTime.Now;
}
else
{
//Message NON OK
EtatGrafcet_loop = 20;
}
inputBuffer = "";
}
}
break;
case 4: //envoi trame readCom
if (envoiWrite != "")
EtatGrafcet_loop = 10; //envoi write
else
{
if (DateTime.Now > timer_send.AddMilliseconds(50)) //Temps entre trames
{
reponseLen = ComSGC400.Create_Trame_ReadCom(ref bufferEnvoi);
if (bufferEnvoi.Length > 0)
{
inputBuffer = "";
Xamarin.Forms.MessagingCenter.Send<App, string>((App)Xamarin.Forms.Application.Current, "WriteDatas", bufferEnvoi);
timer_send = DateTime.Now;
EtatGrafcet_loop = 5;
}
}
}
break;
case 5: //attente reponse trame readCom
if (DateTime.Now > timer_send.AddMilliseconds(TIMOUT_COM_MS)) {
EtatGrafcet_loop = 21; break;
} //timeout
if (inputBuffer != null)
{
ComSGC400.Trame_RechercheDebutTrame(ref inputBuffer);
if (inputBuffer.Length >= reponseLen)
{
inputBuffer = inputBuffer.Substring(0, reponseLen);
res = ComSGC400.Update_Enneigeur( inputBuffer, ref _Enneigeur1);
if (res == true)
{
//Message OK
bufferEnvoi = "";
EtatGrafcet_loop = 2; //Retour read main
erreur_cryptage = 0;
_Enneigeur1.LastRead = DateTime.Now;
if (_Enneigeur1.Reset)
Send_value_bluetooth("reset", false);
}
else
{
//Message NON OK
EtatGrafcet_loop = 21;
}
inputBuffer = "";
}
}
break;
case 10: //envoi trame Write
inputBuffer = "";
Xamarin.Forms.MessagingCenter.Send<App, string>((App)Xamarin.Forms.Application.Current, "WriteDatas", envoiWrite);
timer_send = DateTime.Now;
EtatGrafcet_loop =11;
break;
case 11: //attente reponse trame Write
if (DateTime.Now > timer_send.AddMilliseconds(TIMOUT_COM_MS)) { EtatGrafcet_loop = 20; break; } //timeout
if (inputBuffer != null)
{
ComSGC400.Trame_RechercheDebutTrame(ref inputBuffer);
if (inputBuffer.Length >= 16)
{
bufferEnvoi = "";
if (envoiContinu)
{
EtatGrafcet_loop = 10; //envoi continu
}
else
{
envoiWrite = "";
EtatGrafcet_loop = 2; //Retour read main
}
erreur_cryptage = 0;
_Enneigeur1.LastWrite = DateTime.Now;
inputBuffer = "";
}
}
break;
case 20://Erreur de reception trame readMain
bufferEnvoi = "";
erreur_cryptage = erreur_cryptage + 1;
envoiWrite = "";
if (erreur_cryptage > 3)
{
_Enneigeur1.ResetValues();
erreur_cryptage = 0;
EtatGrafcet_loop = 0;
}
else
{
EtatGrafcet_loop = 2;
}
break;
case 21://Erreur de reception trame readCom
bufferEnvoi = "";
erreur_cryptage = erreur_cryptage + 1;
envoiWrite = "";
if (erreur_cryptage > 3)
{
_Enneigeur1.ResetValues();
erreur_cryptage = 0;
EtatGrafcet_loop = 0;
}
else
{
EtatGrafcet_loop = 4;
}
break;
default:
EtatGrafcet_loop = 0;
break;
}
await Task.Delay(10);
}
EtatGrafcet_loop = 0;
}
public void Send_value_bluetooth(string nom_param, bool value)
{
if (value == true) Send_value_bluetooth( nom_param, 1); else Send_value_bluetooth(nom_param, 0);
}
public void Send_value_bluetooth(string nom_param, double value)
{
//la modif de Enneigeur1 entraine l'envoi d'une trame WRITE en bluetooth (si la liaison est OK)
string bufferEnvoi = "";
if (EtatGrafcet_loop >= 2)
{
switch(nom_param.ToLower())
{
Cases...
}
}
}
public void ExitApp()
{
// Disconnect from bth device
DependencyService.Get<IBtInterface>().Disconnect();
MessagingCenter.Unsubscribe<App, string>(this, "ReadDatas");
IsConnected = false;
if (_ct != null)
{
System.Diagnostics.Debug.WriteLine("Send a cancel to task!");
_ct.Cancel();
}
}
public GlobalViewModel()
{
Resources = new LocalizedResources(typeof(AppResources), Enneigeur1._SelectedLanguageKey
);
MessagingCenter.Subscribe<App>(this, "Sleep", (obj) =>
{
// When the app "sleep", I close the connection with bluetooth
if (IsConnected)
DependencyService.Get<IBtInterface>().Disconnect();
});
MessagingCenter.Subscribe<App>(this, "Resume", (obj) =>
{
// When the app "resume" I try to restart the connection with bluetooth
if (IsConnected)
DependencyService.Get<IBtInterface>().Connect(SelectedBthDevice);
});
this.InitCommand = new Command(() =>
{
// Try to connect to a bth device
DependencyService.Get<IBtInterface>().Init();
});
this.ConnectCommand = new Command(() =>
{
EtatGrafcet_loop = 0;
// Try to connect to a bth device
IsConnected = DependencyService.Get<IBtInterface>().Connect(SelectedBthDevice);
//second essai
if (IsConnected == false) IsConnected = DependencyService.Get<IBtInterface>().Connect(SelectedBthDevice);
if (IsConnected == true)
{
// initialisation des echange de données
MessagingCenter.Subscribe<App, string>(this, "ReadDatas", (sender, arg) =>
{
//inputBuffer = inputBuffer + arg;
inputBuffer = inputBuffer + arg;
});
Task.Run(async () => Loop());
}else
{
//erreur aux 2 tentatives de connection
//Task.Run(async () => ((App)Application.Current).OpenTextBoxDialog());
((App)Application.Current).OpenTextBoxDialog();
}
});
this.DisconnectCommand = new Command(() =>
{
EtatGrafcet_loop = 0;
ExitApp();
});
this.RefreshListDeviceCommand = new Command(() =>
{
try
{
ListOfDevices = DependencyService.Get<IBtInterface>().PairedDevices();
}
catch (Exception ex)
{
Application.Current.MainPage.DisplayAlert("Attention", ex.Message, "Ok");
}
});
// At startup, I load all paired devices
try
{
ListOfDevices = DependencyService.Get<IBtInterface>().PairedDevices();
}
catch (Exception ex)
{
Application.Current.MainPage.DisplayAlert("Attention", ex.Message, "Ok");
}
}
private object Await(App current)
{
throw new NotImplementedException();
}
public ICommand InitCommand { get; protected set; }
public ICommand ConnectCommand { get; protected set; }
public ICommand DisconnectCommand { get; protected set; }
public ICommand RefreshListDeviceCommand { get; protected set; }
public void SaveState(IDictionary<string, object> dictionary)
{
dictionary["SavedLanguage"] = Enneigeur1._SelectedLanguageKey;
dictionary["SavedUnitySystem_Pression"] = Enneigeur1.SelectedUnitySystem_Presure;
dictionary["SavedUnitySystem_Debit"] = Enneigeur1.SelectedUnitySystem_Flow;
dictionary["SavedUnitySystem_Temperature"] = Enneigeur1.SelectedUnitySystem_Temperature;
}
public void RestoreState(IDictionary<string, object> dictionary)
{
Enneigeur1.SelectedUnitySystem_Presure = GetDictionaryEntry(dictionary, "SavedUnitySystem_Pression", "Bars");
Enneigeur1.SelectedUnitySystem_Flow = GetDictionaryEntry(dictionary, "SavedUnitySystem_Debit", "m3/h");
Enneigeur1.SelectedUnitySystem_Temperature = GetDictionaryEntry(dictionary, "SavedUnitySystem_Temperature", "°C");
Enneigeur1.SelectedLanguageKey = GetDictionaryEntry(dictionary, "SavedLanguage", "en");
}
public T GetDictionaryEntry<T>(IDictionary<string, object> dictionary,
string key, T defaultValue)
{
if (dictionary.ContainsKey(key))
return (T)dictionary[key];
return defaultValue;
}
}
Related
In continuation to my previous question C# Confluent.Kafka SetValueDeserializer object deserialization, I have tried creating my custom deserializer to deserialize protobuf message but getting this error:
System.InvalidOperationException: 'Type is not expected, and no contract can be inferred: Ileco.Chimp.Proto.FinalValue'
on line:
return Serializer.Deserialize<T>(stream);
Here my consumer and deserializer:
class Worker
{
public static void Consumer(string brokerList, string connStr, string consumergroup, string topic, string cacertlocation)
{
var config = new ConsumerConfig
{
BootstrapServers = brokerList,
SecurityProtocol = SecurityProtocol.SaslSsl,
SocketTimeoutMs = 60000, //this corresponds to the Consumer config `request.timeout.ms`
SessionTimeoutMs = 30000,
SaslMechanism = SaslMechanism.Plain,
SaslUsername = "$ConnectionString",
SaslPassword = connStr,
SslCaLocation = cacertlocation,
GroupId = consumergroup,
AutoOffsetReset = AutoOffsetReset.Earliest,
BrokerVersionFallback = "1.0.0", //Event Hubs for Kafka Ecosystems supports Kafka v1.0+, a fallback to an older API will fail
//Debug = "security,broker,protocol" //Uncomment for librdkafka debugging information
};
using (var consumer = new ConsumerBuilder<string, FinalValue>(config)
.SetKeyDeserializer(Deserializers.Utf8)
.SetValueDeserializer(new MyCustomDeserializer<FinalValue>())
.SetErrorHandler((_, e) => Console.WriteLine($"Error: {e.Reason}"))
.Build())
{
CancellationTokenSource cts = new CancellationTokenSource();
Console.CancelKeyPress += (_, e) => { e.Cancel = true; cts.Cancel(); };
consumer.Subscribe(topic);
Console.WriteLine("Consuming messages from topic: " + topic + ", broker(s): " + brokerList);
while (true)
{
try
{
var msg = consumer.Consume(cts.Token);
Console.WriteLine($"Received: '{msg.Message.Value}'");
//var bytes = Encoding.ASCII.GetBytes(msg.Message.Value);
//var fv = FromByteArray<ProtobufMsg>(bytes);
//var proto = ProtoDeserialize<ProtobufMsg>(bytes);
}
catch (ConsumeException e)
{
Console.WriteLine($"Consume error: {e.Error.Reason}");
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}
}
}
public class MyCustomDeserializer<T> : IDeserializer<T>
{
public T Deserialize(ReadOnlySpan<byte> data, bool isNull, Confluent.Kafka.SerializationContext context)
{
using (var stream = new MemoryStream(data.ToArray()))
{
return Serializer.Deserialize<T>(stream);
}
}
}
FinalValue.proto
syntax = "proto3";
package ileco.chimp.proto;
import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";
option java_package = "ileco.chimp.proto";
option java_outer_classname = "FinalValueProtos";
message FinalValue {
google.protobuf.Timestamp timestamp = 1;
uint32 inputId = 2;
google.protobuf.DoubleValue value = 3;
uint32 sourceId = 4;
string inputGuid = 5;
}
FinalValue.cs
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: proto/FinalValue.proto
#pragma warning disable 1591, 0612, 3021
#region Designer generated code
using pb = global::Google.Protobuf;
using pbc = global::Google.Protobuf.Collections;
using pbr = global::Google.Protobuf.Reflection;
using scg = global::System.Collections.Generic;
namespace Ileco.Chimp.Proto {
/// <summary>Holder for reflection information generated from proto/FinalValue.proto</summary>
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class FinalValueReflection {
#region Descriptor
/// <summary>File descriptor for proto/FinalValue.proto</summary>
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
}
private static pbr::FileDescriptor descriptor;
static FinalValueReflection() {
byte[] descriptorData = global::System.Convert.FromBase64String(
string.Concat(
"ChZwcm90by9GaW5hbFZhbHVlLnByb3RvEhFpbGVjby5jaGltcC5wcm90bxof",
"Z29vZ2xlL3Byb3RvYnVmL3RpbWVzdGFtcC5wcm90bxoeZ29vZ2xlL3Byb3Rv",
"YnVmL3dyYXBwZXJzLnByb3RvIp4BCgpGaW5hbFZhbHVlEi0KCXRpbWVzdGFt",
"cBgBIAEoCzIaLmdvb2dsZS5wcm90b2J1Zi5UaW1lc3RhbXASDwoHaW5wdXRJ",
"ZBgCIAEoDRIrCgV2YWx1ZRgDIAEoCzIcLmdvb2dsZS5wcm90b2J1Zi5Eb3Vi",
"bGVWYWx1ZRIQCghzb3VyY2VJZBgEIAEoDRIRCglpbnB1dEd1aWQYBSABKAlC",
"JQoRaWxlY28uY2hpbXAucHJvdG9CEEZpbmFsVmFsdWVQcm90b3NiBnByb3Rv",
"Mw=="));
descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
new pbr::FileDescriptor[] { global::Google.Protobuf.WellKnownTypes.TimestampReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.WrappersReflection.Descriptor, },
new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] {
new pbr::GeneratedClrTypeInfo(typeof(global::Ileco.Chimp.Proto.FinalValue), global::Ileco.Chimp.Proto.FinalValue.Parser, new[]{ "Timestamp", "InputId", "Value", "SourceId", "InputGuid" }, null, null, null)
}));
}
#endregion
}
#region Messages
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public sealed partial class FinalValue : pb::IMessage<FinalValue> {
private static readonly pb::MessageParser<FinalValue> _parser = new pb::MessageParser<FinalValue>(() => new FinalValue());
public static pb::MessageParser<FinalValue> Parser { get { return _parser; } }
public static pbr::MessageDescriptor Descriptor {
get { return global::Ileco.Chimp.Proto.FinalValueReflection.Descriptor.MessageTypes[0]; }
}
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
public FinalValue() {
OnConstruction();
}
partial void OnConstruction();
public FinalValue(FinalValue other) : this() {
Timestamp = other.timestamp_ != null ? other.Timestamp.Clone() : null;
inputId_ = other.inputId_;
Value = other.Value;
sourceId_ = other.sourceId_;
inputGuid_ = other.inputGuid_;
}
public FinalValue Clone() {
return new FinalValue(this);
}
/// <summary>Field number for the "timestamp" field.</summary>
public const int TimestampFieldNumber = 1;
private global::Google.Protobuf.WellKnownTypes.Timestamp timestamp_;
public global::Google.Protobuf.WellKnownTypes.Timestamp Timestamp {
get { return timestamp_; }
set {
timestamp_ = value;
}
}
/// <summary>Field number for the "inputId" field.</summary>
public const int InputIdFieldNumber = 2;
private uint inputId_;
public uint InputId {
get { return inputId_; }
set {
inputId_ = value;
}
}
/// <summary>Field number for the "value" field.</summary>
public const int ValueFieldNumber = 3;
private static readonly pb::FieldCodec<double?> _single_value_codec = pb::FieldCodec.ForStructWrapper<double>(26);
private double? value_;
public double? Value {
get { return value_; }
set {
value_ = value;
}
}
/// <summary>Field number for the "sourceId" field.</summary>
public const int SourceIdFieldNumber = 4;
private uint sourceId_;
public uint SourceId {
get { return sourceId_; }
set {
sourceId_ = value;
}
}
/// <summary>Field number for the "inputGuid" field.</summary>
public const int InputGuidFieldNumber = 5;
private string inputGuid_ = "";
public string InputGuid {
get { return inputGuid_; }
set {
inputGuid_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
}
}
public override bool Equals(object other) {
return Equals(other as FinalValue);
}
public bool Equals(FinalValue other) {
if (ReferenceEquals(other, null)) {
return false;
}
if (ReferenceEquals(other, this)) {
return true;
}
if (!object.Equals(Timestamp, other.Timestamp)) return false;
if (InputId != other.InputId) return false;
if (Value != other.Value) return false;
if (SourceId != other.SourceId) return false;
if (InputGuid != other.InputGuid) return false;
return true;
}
public override int GetHashCode() {
int hash = 1;
if (timestamp_ != null) hash ^= Timestamp.GetHashCode();
if (InputId != 0) hash ^= InputId.GetHashCode();
if (value_ != null) hash ^= Value.GetHashCode();
if (SourceId != 0) hash ^= SourceId.GetHashCode();
if (InputGuid.Length != 0) hash ^= InputGuid.GetHashCode();
return hash;
}
public override string ToString() {
return pb::JsonFormatter.ToDiagnosticString(this);
}
public void WriteTo(pb::CodedOutputStream output) {
if (timestamp_ != null) {
output.WriteRawTag(10);
output.WriteMessage(Timestamp);
}
if (InputId != 0) {
output.WriteRawTag(16);
output.WriteUInt32(InputId);
}
if (value_ != null) {
_single_value_codec.WriteTagAndValue(output, Value);
}
if (SourceId != 0) {
output.WriteRawTag(32);
output.WriteUInt32(SourceId);
}
if (InputGuid.Length != 0) {
output.WriteRawTag(42);
output.WriteString(InputGuid);
}
}
public int CalculateSize() {
int size = 0;
if (timestamp_ != null) {
size += 1 + pb::CodedOutputStream.ComputeMessageSize(Timestamp);
}
if (InputId != 0) {
size += 1 + pb::CodedOutputStream.ComputeUInt32Size(InputId);
}
if (value_ != null) {
size += _single_value_codec.CalculateSizeWithTag(Value);
}
if (SourceId != 0) {
size += 1 + pb::CodedOutputStream.ComputeUInt32Size(SourceId);
}
if (InputGuid.Length != 0) {
size += 1 + pb::CodedOutputStream.ComputeStringSize(InputGuid);
}
return size;
}
public void MergeFrom(FinalValue other) {
if (other == null) {
return;
}
if (other.timestamp_ != null) {
if (timestamp_ == null) {
timestamp_ = new global::Google.Protobuf.WellKnownTypes.Timestamp();
}
Timestamp.MergeFrom(other.Timestamp);
}
if (other.InputId != 0) {
InputId = other.InputId;
}
if (other.value_ != null) {
if (value_ == null || other.Value != 0D) {
Value = other.Value;
}
}
if (other.SourceId != 0) {
SourceId = other.SourceId;
}
if (other.InputGuid.Length != 0) {
InputGuid = other.InputGuid;
}
}
public void MergeFrom(pb::CodedInputStream input) {
uint tag;
while ((tag = input.ReadTag()) != 0) {
switch(tag) {
default:
input.SkipLastField();
break;
case 10: {
if (timestamp_ == null) {
timestamp_ = new global::Google.Protobuf.WellKnownTypes.Timestamp();
}
input.ReadMessage(timestamp_);
break;
}
case 16: {
InputId = input.ReadUInt32();
break;
}
case 26: {
double? value = _single_value_codec.Read(input);
if (value_ == null || value != 0D) {
Value = value;
}
break;
}
case 32: {
SourceId = input.ReadUInt32();
break;
}
case 42: {
InputGuid = input.ReadString();
break;
}
}
}
}
}
#endregion
}
#endregion Designer generated code
As I noted yesterday, you appear to have used the Google .proto processing tools (protoc), but are using protobuf-net; if you want to use protobuf-net, similar command-line/IDE/build/etc tools exist that are compatible with the protobuf-net library, or you can use https://protogen.marcgravell.com/ for ad-hoc usage (to avoid having to install anything). Alternatively: continue using the Google schema tools, but use the Google library. Basically: they need to match.
The only minor gotcha here is that protobuf-net does not currently have explicit inbuilt support for DoubleValue; for reference: this can be considered as simply:
namespace Google.Protobuf.WellKnownTypes
{
[ProtoContract]
public sealed class DoubleValue
{
[ProtoMember(1)]
public double Value {get;set;}
}
}
I should probably find time to take all the types from wrappers.proto and allow them as double?, float?, long? etc - but it will need an additional marker, as Nullable<T> is already handled but with a different meaning (i.e. optional in .proto terms)
As the title says, my empty itself in middle of code.
I do not do any background/thread work so it couldn't empty itself in middle of executing code. It empty itself on one particular point every time.
ObservableCollection is proizvod.GrupeIPodgrupe in my case
Here is where it happens:
private void NamestiGrupeIPodgrupe()
{
foreach (TreeNode n in treeView1.Nodes)
{
if (proizvod.GrupeIPodGrupe.Grupe.Any(g => g.Id == (int)n.Tag))
{
n.Checked = true;
foreach (TreeNode n1 in n.Nodes) //Here it empty
{
if (proizvod.GrupeIPodGrupe.Podgrupe.Any(pg => pg.PodGrupaId == (int)n1.Tag))
{
n1.Checked = true;
}
}
}
else
n.Checked = false;
}
}
Here are images when I debug it step by step:
Here is full code of my Form class where this problem is happening:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Magacin.Sajt;
namespace Magacin
{
public partial class Sajt_Proizvod : Form
{
private Proizvod proizvod;
private Komercijalno.Roba roba;
public Sajt_Proizvod(int proizvodID)
{
InitializeComponent();
UcitajGrupeIPodgrupe();
this.roba = new Komercijalno.Roba(proizvodID);
UcitajProizvod(proizvodID);
k_jm_txt.Text = roba.Jm;
k_katBr_txt.Text = roba.KatBr;
k_naziv_txt.Text = roba.Naziv;
k_pdv_txt.Text = String.Format("{0:#.00}%", roba.Pdv);
k_robaid_txt.Text = roba.RobaId.ToString();
NamestiGrupeIPodgrupe();
}
private void PodesiPremaPravima()
{
if (!Korisnik.ImaPravo(200103))
{
w_naziv_txt.Enabled = false;
w_naziv_txt.BackColor = SystemColors.Info;
slika_txt.Enabled = false;
slika_txt.BackColor = SystemColors.Info;
w_opis_rtxt.Enabled = false;
w_opis_rtxt.BackColor = SystemColors.Info;
akcijskiArtikal_cb.Enabled = false;
akcijskiArtikal_cb.BackColor = SystemColors.Info;
aktivan_cb.Enabled = false;
aktivan_cb.BackColor = SystemColors.Info;
button2.BackColor = SystemColors.Info;
}
else
{
w_naziv_txt.Enabled = true;
w_naziv_txt.BackColor = SystemColors.Window;
slika_txt.Enabled = true;
slika_txt.BackColor = SystemColors.Window;
w_opis_rtxt.Enabled = true;
w_opis_rtxt.BackColor = SystemColors.Window;
akcijskiArtikal_cb.Enabled = true;
akcijskiArtikal_cb.BackColor = SystemColors.Window;
aktivan_cb.Enabled = true;
aktivan_cb.BackColor = SystemColors.Window;
}
}
private void NamestiGrupeIPodgrupe()
{
foreach (TreeNode n in treeView1.Nodes)
{
if (proizvod.GrupeIPodGrupe.Grupe.Any(g => g.Id == (int)n.Tag))
{
n.Checked = true;
foreach (TreeNode n1 in n.Nodes)
{
if (proizvod.GrupeIPodGrupe.Podgrupe.Any(pg => pg.PodGrupaId == (int)n1.Tag))
{
n1.Checked = true;
}
}
}
else
n.Checked = false;
}
}
private void UcitajProizvod()
{
proizvod = new Proizvod(proizvod.PROIZVODID);
w_proizvodId_txt.Text = proizvod.PROIZVODID.ToString();
w_naziv_txt.Text = proizvod.NAZIV.ToString();
w_katBr_txt.Text = proizvod.KATBR.ToString();
w_pdv_txt.Text = String.Format("{0}%", proizvod.PDV);
w_jm_txt.Text = proizvod.JM.ToString();
slika_txt.Text = proizvod.SLIKA.ToString();
kratakOpis_txt.Text = proizvod.KratakOpis;
w_opis_rtxt.Text = proizvod.OPIS;
akcijskiArtikal_cb.Checked = (proizvod.AKCIJSKI_ARTIKAL == 1) ? true : false;
aktivan_cb.Checked = (proizvod.AKTIVAN == 1) ? true : false;
}
private void UcitajProizvod(int poizvodId)
{
proizvod = new Proizvod(poizvodId);
w_proizvodId_txt.Text = proizvod.PROIZVODID.ToString();
w_naziv_txt.Text = proizvod.NAZIV.ToString();
w_katBr_txt.Text = proizvod.KATBR.ToString();
w_pdv_txt.Text = String.Format("{0}%", proizvod.PDV);
w_jm_txt.Text = proizvod.JM.ToString();
slika_txt.Text = proizvod.SLIKA.ToString();
kratakOpis_txt.Text = proizvod.KratakOpis;
w_opis_rtxt.Text = proizvod.OPIS;
akcijskiArtikal_cb.Checked = (proizvod.AKCIJSKI_ARTIKAL == 1) ? true : false;
aktivan_cb.Checked = (proizvod.AKTIVAN == 1) ? true : false;
}
private void UcitajGrupeIPodgrupe()
{
List<Grupa> grupe = Grupa.ListaSvihGrupa();
foreach(Grupa gp in grupe)
{
TreeNode tn = new TreeNode(gp.Naziv);
List<PodGrupa> pg = PodGrupa.ListaSvihPodgrupa(gp.Id);
if(pg != null && pg.Count > 0)
{
foreach(PodGrupa pgp in pg)
{
TreeNode tn1 = new TreeNode(pgp.Naziv);
tn1.Tag = pgp.PodGrupaId;
tn1.ToolTipText = "PodGrupa";
tn.Nodes.Add(tn1);
}
}
tn.Tag = gp.Id;
tn.ToolTipText = "Grupa";
treeView1.Nodes.Add(tn);
}
}
private void sacuvaj_btn_Click(object sender, EventArgs e)
{
if (Korisnik.ImaPravo(200103))
{
proizvod.Update();
MessageBox.Show(proizvod.errorMessage);
}
else
{
MessageBox.Show("Nemate pravo pristupa modulu [ 200103 ] ");
}
}
private void odbaci_btn_Click(object sender, EventArgs e)
{
UcitajProizvod();
}
private void novi_btn_Click(object sender, EventArgs e)
{
using (Sajt_NoviProizvod np = new Sajt_NoviProizvod())
{
np.ShowDialog();
}
}
private void w_naziv_txt_TextChanged(object sender, EventArgs e)
{
proizvod.NAZIV = w_naziv_txt.Text;
sacuvaj_btn.Enabled = true;
odbaci_btn.Enabled = true;
}
private void w_jm_txt_TextChanged(object sender, EventArgs e)
{
proizvod.JM = w_jm_txt.Text;
}
private void w_opis_rtxt_TextChanged(object sender, EventArgs e)
{
proizvod.OPIS = w_opis_rtxt.Text;
sacuvaj_btn.Enabled = true;
odbaci_btn.Enabled = true;
}
private void akcijskiArtikal_cb_CheckedChanged(object sender, EventArgs e)
{
proizvod.AKCIJSKI_ARTIKAL = Convert.ToInt32(akcijskiArtikal_cb.Checked);
sacuvaj_btn.Enabled = true;
odbaci_btn.Enabled = true;
}
private void aktivan_cb_CheckedChanged(object sender, EventArgs e)
{
proizvod.AKTIVAN = Convert.ToInt32(aktivan_cb.Checked);
sacuvaj_btn.Enabled = true;
odbaci_btn.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
using (Sajt_Paketi sp = new Sajt_Paketi(proizvod.PROIZVODID))
{
sp.ShowDialog();
}
}
private void slika_txt_TextChanged(object sender, EventArgs e)
{
proizvod.SLIKA = slika_txt.Text;
sacuvaj_btn.Enabled = true;
odbaci_btn.Enabled = true;
}
private void kratakOpis_txt_TextChanged(object sender, EventArgs e)
{
proizvod.KratakOpis = kratakOpis_txt.Text;
sacuvaj_btn.Enabled = true;
odbaci_btn.Enabled = true;
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Node.Parent != null)
{
if (e.Node.Parent.Parent != null)
{
e.Node.Parent.Parent.Checked = true;
}
e.Node.Parent.Checked = true;
}
if (e.Node.Tag.ToString() == "Grupa")
{
foreach (TreeNode tn in e.Node.Nodes)
{
tn.Checked = false;
}
}
ProveriGrupeIPodgrupe();
sacuvaj_btn.Enabled = true;
odbaci_btn.Enabled = true;
}
private void ProveriGrupeIPodgrupe()
{
proizvod.GrupeIPodGrupe.Reset();
foreach (TreeNode tn in treeView1.Nodes)
{
if (tn.Checked)
{
foreach (TreeNode tn1 in tn.Nodes)
{
if (tn1.Checked)
{
proizvod.GrupeIPodGrupe.Podgrupe.Add(new PodGrupa { PodGrupaId = Convert.ToInt32(tn1.Tag), GrupaId = Convert.ToInt32(tn.Tag) });
}
}
}
}
}
private void odbaci_btn_EnabledChanged(object sender, EventArgs e)
{
if (odbaci_btn.Enabled)
odbaci_btn.BackColor = Color.White;
else
odbaci_btn.BackColor = Color.DimGray;
}
private void sacuvaj_btn_EnabledChanged(object sender, EventArgs e)
{
if (sacuvaj_btn.Enabled)
sacuvaj_btn.BackColor = Color.White;
else
sacuvaj_btn.BackColor = Color.DimGray;
}
}
}
Here is my Proizvod class
public class Proizvod
{
#region Variables
public int PROIZVODID { get { return _PROIZVODID; } set { _PROIZVODID = value; } }
public string NAZIV { get { return _NAZIV; } set { _NAZIV = value; } }
public string OPIS { get { return _OPIS; } set { _OPIS = value; } }
public string JM { get { return _JM; } set { _JM = value; } }
public int AKCIJSKI_ARTIKAL { get { return _AKCIJSKI_ARTIKAL; } set { _AKCIJSKI_ARTIKAL = value; } }
public int AKTIVAN { get { return _AKTIVAN; } set { _AKTIVAN = value; } }
public string KATBR { get { return _KATBR; } set { _KATBR = value; } }
public double PDV { get { return _PDV; } set { _PDV = value; } }
public string SLIKA { get { return _SLIKA; } set { _SLIKA = value; } }
public string KratakOpis { get { return _KratakOpis; } set { _KratakOpis = value; } }
public GrupeIPodgrupe GrupeIPodGrupe { get { return _GrupeIPodGrupe; } }
public List<Paket> Paketi { get { return _Paketi; } }
public string errorMessage { get { return _errorMessage; } }
private int _PROIZVODID;
private string _NAZIV;
private string _OPIS;
private string _JM;
private int _AKCIJSKI_ARTIKAL;
private int _AKTIVAN;
private string _KATBR;
private double _PDV;
private string _SLIKA;
private string _KratakOpis;
private GrupeIPodgrupe _GrupeIPodGrupe;
private List<Paket> _Paketi;
private string fileNameSlika;
private string pathSlika;
string _errorMessage = "Sve je ok!";
#endregion
#region classes
public class GrupeIPodgrupe
{
public ReadOnlyCollection<Grupa> Grupe
{
get
{
return _Grupe.AsReadOnly();
}
}
public ObservableCollection<PodGrupa> Podgrupe
{
get
{
return _PodGrupe;
}
set
{
_PodGrupe = value;
}
}
private List<Grupa> _Grupe;
private ObservableCollection<PodGrupa> _PodGrupe;
private void HandleChange(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (PodGrupa pg in e.NewItems)
{
if (_Grupe.Count < 1 || !_Grupe.Any(g => g.Id == pg.GrupaId))
{
_Grupe.Add(new Grupa { Id = pg.GrupaId });
}
}
}
}
public GrupeIPodgrupe()
{
_PodGrupe = new ObservableCollection<PodGrupa>();
_PodGrupe.CollectionChanged += HandleChange;
}
public GrupeIPodgrupe(int proizvodId)
{
this._Grupe = Grupa.ListaGrupaProizvoda(proizvodId);
if(this.Grupe.Count > 0)
{
this.Podgrupe = PodGrupa.ListaPodgrupaProizvoda(Grupe[0].Id, proizvodId);
}
}
public GrupeIPodgrupe(TreeView treeView)
{
_Grupe = new List<Grupa>();
_PodGrupe = new ObservableCollection<PodGrupa>();
_PodGrupe.CollectionChanged += HandleChange;
foreach (TreeNode n in treeView.Nodes)
{
if(n.Checked)
{
foreach(TreeNode n1 in n.Nodes)
{
if(n1.Checked)
_PodGrupe.Add(new PodGrupa { GrupaId = (int)n.Tag, PodGrupaId = (int)n1.Tag });
}
}
}
}
public void Reset()
{
_Grupe = new List<Grupa>();
_PodGrupe = new ObservableCollection<PodGrupa>();
}
}
public class Paket
{
public int PaketId;
public int ProizvodId;
public double CenaBezPdv;
public double Kolicina;
}
#endregion
private Proizvod()
{
}
/// <summary>
/// Kreira klasu sa podacima proizvoda sa web-a
/// </summary>
/// <param name="ROBAID">Unikatni ID za stavku/proizvod/robu</param>
public Proizvod(int ROBAID)
{
if(ROBAID == null ) { throw new Exception("ROBAID ne moze biti NULL!"); }
string url = "censoredLink";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string resp = new StreamReader(response.GetResponseStream()).ReadToEnd();
switch (resp)
{
case "0": //Nije pronadjen proizvod
_errorMessage = "Proizvod nije pronadjen u web bazi!";
break;
default:
Proizvod p = JsonConvert.DeserializeObject<Proizvod>(resp);
if(p == null)
{
MessageBox.Show("Unhandled error!");
return;
}
this._PROIZVODID = p.PROIZVODID;
this._NAZIV = p.NAZIV;
this._OPIS = p.OPIS;
this._JM = p.JM;
this._AKCIJSKI_ARTIKAL = p.AKCIJSKI_ARTIKAL;
this._AKTIVAN = p.AKTIVAN;
this._KATBR = p.KATBR;
this._PDV = p.PDV;
this._SLIKA = p.SLIKA;
this._KratakOpis = p.KratakOpis;
this._GrupeIPodGrupe = new GrupeIPodgrupe(p.PROIZVODID);
break;
}
}
public Proizvod(int RobaId, string Naziv, string Opis, string Jm, bool AkcijskiArtikal, bool Aktivan, string KatBr, double PDV, string Slika, string KratakOpis, GrupeIPodgrupe gip, List<Paket> paketi, string fileNameSlika, string filePathSlika)
{
this._PROIZVODID = RobaId;
this._NAZIV = Naziv;
this._OPIS = Opis;
this._JM = Jm;
this._AKCIJSKI_ARTIKAL = (AkcijskiArtikal) ? 1 : 0;
this._AKTIVAN = (Aktivan) ? 1 : 0;
this._KATBR = KatBr;
this._PDV = PDV;
this._SLIKA = Slika;
this._KratakOpis = KratakOpis;
this._GrupeIPodGrupe = gip;
this._Paketi = paketi;
this.fileNameSlika = fileNameSlika;
this.pathSlika = filePathSlika;
}
/// <summary>
/// Updateuje trenutne vrednosti proizvoda na sajt!
/// </summary>
public void Update()
{
#region Proizvod table
string url = String.Format("censoredLink");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string resp = new StreamReader(response.GetResponseStream()).ReadToEnd();
switch (resp)
{
case "0": //Nije pronadjen proizvod
_errorMessage = "Doslo je do greske!";
break;
case "1":
_errorMessage = "Uspesno updateovan prozvod na WEB-u";
break;
case "2":
_errorMessage = "Proizvod sa tim ID-em nije pronadjen u bazi!";
break;
default:
break;
}
#endregion
DeGrupisi();
Grupisi();
}
public void Publish()
{
PublishProizvod();
Grupisi();
PublishPakete();
Thread t = new Thread(PublishSliku);
t.Start();
}
public static List<Proizvod> SviProizvodi()
{
List<Proizvod> list = new List<Proizvod>();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
string url = "censoredLink";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string resp = new StreamReader(response.GetResponseStream()).ReadToEnd();
switch (resp)
{
case "0": //Nije pronadjen proizvod
break;
default:
list = JsonConvert.DeserializeObject<List<Proizvod>>(resp);
break;
}
return list;
}
/// <summary>
/// Skida proizvodu bilo kakvu grupu i podgrupu
/// </summary>
private void DeGrupisi()
{
string url = "censoredLink";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string resp = new StreamReader(response.GetResponseStream()).ReadToEnd();
switch (resp)
{
case "0":
_errorMessage = "Doslo je do greske prilikom brisanja proizvoda iz grupa i podgrupa";
break;
case "1":
_errorMessage = "Uspesno degrupisani proizvodi!";
break;
default:
_errorMessage = "Unhandled error";
break;
}
}
private void Grupisi()
{
foreach(PodGrupa pg in GrupeIPodGrupe.Podgrupe)
{
string url = string.Format("censoredLink");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string resp = new StreamReader(response.GetResponseStream()).ReadToEnd();
switch (resp)
{
case "0":
_errorMessage = "Doslo je do greske prilikom grupisanja proizvoda";
break;
case "1":
_errorMessage = "Proizvod je uspesno grupisan!";
break;
case "2":
_errorMessage = "Proizvod je vec u grupi!";
break;
default:
_errorMessage = "Unhandled error";
break;
}
}
}
private void PublishProizvod()
{
string url = string.Format("censoredLink");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string resp = new StreamReader(response.GetResponseStream()).ReadToEnd();
switch (resp)
{
case "0":
_errorMessage = "Doslo je do greske prilikom kreiranja proizvoda";
break;
case "1":
_errorMessage = "Proizvod je uspesno kreiran!";
break;
case "2":
_errorMessage = "Proizvod vec postoji u web bazi!";
break;
default:
_errorMessage = "Unhandled error";
break;
}
}
private void PublishPakete()
{
foreach (Proizvod.Paket p in Paketi)
{
string url = string.Format("censoredLink");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string resp = new StreamReader(response.GetResponseStream()).ReadToEnd();
switch (resp)
{
case "0":
_errorMessage = "Doslo je do greske prilikom dodavanja paketa";
break;
case "1":
_errorMessage = "Paketi uspesno dodati!";
break;
default:
_errorMessage = "Unhandled error";
break;
}
}
}
private void PublishSliku()
{
if (!string.IsNullOrWhiteSpace(_SLIKA))
{
M.preventClosing = true;
MessageBox.Show("Dodavanje slike ce potrajati i zavisi od brzine interneta koju imate i nece vam dozvoliti zatvaranje programa!\nDodavanje slike se vrsi u pozadini te mozete nastaviti sa radom u programu!");
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("username", "password");
client.UploadFile("censoredLink");
}
M.preventClosing = false;
}
}
}
Your code is calling:
proizvod.GrupeIPodGrupe.Reset()
which is emptying the ObservableCollection.
I'm making a small game for myself and my family using a MenuStrip as the action commands in the combat. However, I can't quite figure out how to set up the Click function for the magic. Because the menustrip starts off mostly blank, I can't both create the list of spells and targets, and set up the click methods for those spells. I'm at a bit of an impasse...
This is what it looks like
string[] Spells = { };
string[] Elements = { };
string[] Targets = { };
double[] Power = { };
double[] ManaCost = { };
StreamReader strReader = null;
try
{
FileStream flsStream = new FileStream(Application.StartupPath + "\\Spells.txt", FileMode.Open, FileAccess.Read);
strReader = new StreamReader(flsStream);
while (strReader.Peek() != -1)
{
string line = strReader.ReadLine();
string[] lines = line.Split(':');
Array.Resize(ref Spells, Spells.Length + 1);
Array.Resize(ref Elements, Elements.Length + 1);
Array.Resize(ref Targets, Targets.Length + 1);
Array.Resize(ref Power, Power.Length + 1);
Array.Resize(ref ManaCost, ManaCost.Length + 1);
Spells[Spells.Length - 1] = lines[0];
Elements[Elements.Length - 1] = lines[1];
Targets[Targets.Length - 1] = lines[2];
Power[Power.Length - 1] = Convert.ToDouble(lines[3]);
ManaCost[ManaCost.Length - 1] = Convert.ToDouble(lines[4]);
}
for (int x = 0; x < Spells.Length; x++)
{
mnuMagic.DropDownItems.Add(Spells[x]);
mnuMagic.DropDownItems[x].Name = string.Format("tsmi{0}", Spells[x]);
mnuMagic.DropDownItems[x].Text += string.Format(", {0} MP", ManaCost[x]);
if (Targets[x] == "Single")
{
try
{
if (Foe1.Status != "Dead")
{
if (Foe1.Health > Foe1.MaxHealth / 2)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe1, imgEnemyStatus.Images[0]);
else if (Foe1.Health > Foe1.Health / 4)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe1, imgEnemyStatus.Images[1]);
else
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe1, imgEnemyStatus.Images[2]);
}
if (Foe2.Status != "Dead")
{
if (Foe2.Health > Foe2.MaxHealth / 2)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe2, imgEnemyStatus.Images[0]);
else if (Foe2.Health > Foe2.Health / 4)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe2, imgEnemyStatus.Images[1]);
else
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe2, imgEnemyStatus.Images[2]);
}
if (Foe3.Status != "Dead")
{
if (Foe3.Health > Foe3.MaxHealth / 2)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe3, imgEnemyStatus.Images[0]);
else if (Foe3.Health > Foe3.Health / 4)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe3, imgEnemyStatus.Images[1]);
else
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe3, imgEnemyStatus.Images[2]);
}
if (Foe4.Status != "Dead")
{
if (Foe4.Health > Foe4.MaxHealth / 2)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe4, imgEnemyStatus.Images[0]);
else if (Foe4.Health > Foe4.Health / 4)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe4, imgEnemyStatus.Images[1]);
else
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe4, imgEnemyStatus.Images[2]);
}
if (Foe5.Status != "Dead")
{
if (Foe5.Health > Foe5.MaxHealth / 2)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe5, imgEnemyStatus.Images[0]);
else if (Foe5.Health > Foe5.Health / 4)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe5, imgEnemyStatus.Images[1]);
else
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe5, imgEnemyStatus.Images[2]);
}
}
catch { }
}
else if (Targets[x] == "Ally")
{
try
{
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(Fighter1.Name,imgEnemyStatus.Images[0]);
if (Fighter2.Name != null)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(Fighter2.Name);
if (Fighter3.Name != null)
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(Fighter3.Name);
}
catch { }
}
else if (Targets[x] == "All")
{
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add("All Enemies");
}
else if (Targets[x] == "Team")
(mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add("All Allies");
if (Elements[x] == "Fire")
mnuMagic.DropDownItems[x].ForeColor = Color.Red;
else if (Elements[x] == "Water")
mnuMagic.DropDownItems[x].ForeColor = Color.Blue;
else if (Elements[x] == "Earth")
mnuMagic.DropDownItems[x].ForeColor = Color.FromArgb(255, 128, 0);
else if (Elements[x] == "Wind")
mnuMagic.DropDownItems[x].ForeColor = Color.LimeGreen;
if (ManaCost[x] > Fighter1.Mana) { mnuMagic.DropDownItems[x].Enabled = false; }
}
}
catch { }
}
I think you should start dividing your code into subclasses. It will be very helpfull, even for little games. Here is a little bit of code I fiddled together. It does not handle everything you have, but you can easily extend that functionality. If you need additional informations, please tell me.
public partial class Form1 : Form
{
private double CurrentMana { get; set; } = 50;
private List<BasicSpell> m_lstSpells = new List<BasicSpell>();
private List<BasicTarget> m_lstTargets = new List<BasicTarget>();
public Form1()
{
InitializeComponent();
Init();
try
{
ToolStripMenuItem tsmiMagic = new ToolStripMenuItem("Magic");
foreach (BasicSpell spell in m_lstSpells)
{
ToolStripMenuItem tsmiSpell = spell.ToToolStripMenuItem();
if (spell.CanHitAllEnemies)
{
ToolStripItem tsiAll = tsmiSpell.DropDownItems.Add("All");
tsiAll.Click += (sender, arguments) => m_lstTargets.ForEach((target) => TryCastSpell(spell, target));
}
else { } // This spell can not hit all enemies
for (int i = 0; i < m_lstTargets.Count; i++)
{
BasicTarget target = m_lstTargets[i];
ToolStripItem tsiTarget = tsmiSpell.DropDownItems.Add($"Target {i}: {target.Name}");
tsiTarget.Image = target.StatusImage;
tsiTarget.Click += (sender, arguments) => TryCastSpell(spell, target);
}
tsmiMagic.DropDownItems.Add(tsmiSpell);
}
m_tsCombatOptions.Items.Add(tsmiMagic);
}
catch (Exception ex)
{
MessageBox.Show($"{ex}");
}
}
private void TryCastSpell(BasicSpell spell, BasicTarget target)
{
if (!spell.Cast(target))
{
MessageBox.Show($"You can not cast this spell with your current mana!");
}
}
private void Init()
{
m_lstSpells.Add(new Fireball());
m_lstSpells.Add(new Fireball());
m_lstSpells.Add(new Fireball());
m_lstSpells.Add(new Fireball());
m_lstTargets.Add(new Monster("Reißzahn", 500));
m_lstTargets.Add(new Monster("Reißzahn", 500));
m_lstTargets.Add(new Monster("Reißzahn", 500));
m_lstTargets.Add(new Monster("Reißzahn", 500));
m_lstTargets.Add(new Monster("Reißzahn", 500));
}
}
// TODO: POWER?
[Flags]
public enum Element
{
Fire = 0x0,
Earth = 0x1,
Water = 0x2,
Wind = 0x4
}
public abstract class BasicSpell
{
public string Name { get; set; }
public Element DamageType { get; set; }
public double ManaCost { get; set; }
public bool CanHitAllEnemies { get; set; } = false;
public abstract bool Cast(BasicTarget target);
public ToolStripMenuItem ToToolStripMenuItem()
{
ToolStripMenuItem tsmiSpell = new ToolStripMenuItem();
tsmiSpell.Text = $"{this.Name}, {ManaCost} MP";
return tsmiSpell;
}
}
public class Fireball : BasicSpell
{
public override bool Cast(BasicTarget target)
{
// TODO: Check if castable (maybe add caster as parameter)
target.HP -= 500;
return true;
}
}
public abstract class BasicTarget
{
public enum CombatStatus
{
Alive = 0,
Dead = 1
}
public enum TargetRelation
{
Enemy = 0,
Ally = 1
}
public string Name { get; set; }
public double HP { get; set; }
public CombatStatus Status { get; set; }
public TargetRelation Relation { get; set; }
public Image StatusImage => null;
}
public class Monster : BasicTarget
{
public Monster(string strName, int nHP)
{
this.Name = strName;
this.HP = nHP;
this.Status = CombatStatus.Alive;
this.Relation = TargetRelation.Enemy;
}
}
At the starting of GameState.Running, i want to stop input key in 3 seconds, after that, every time when my character take damage, i want to lose control in 1 second, could anyone show me how to do that?
public override void Update(GameTime gameTime, KeyboardState Current, KeyboardState Old)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (TimeGetReady < 0)
{
LockKey = false;
}
else
{
LockKey = true;
TimeGetReady -= elapsed;
}
if (LockKey == false)
{
CurrentKeys = Current;
OldKeys = Old;
}
if (CurrentKeys.IsKeyDown(Keys.P))
{
if (!OldKeys.IsKeyDown(Keys.P))
if (IsPause == true)
IsPause = false;
else
IsPause = true;
}
if (IsPause == true)
{
return;
}
Update(elapsed);
if (CurrentKeys.IsKeyDown(Keys.Left))
{
KeyLeft = true;
if (HPF < 3)
HPF += 2;
PushHorizontal += 5;
Face = -1;
}
else
if (Old.IsKeyDown(Keys.Left))
{
KeyLeft = false;
}
if (CurrentKeys.IsKeyDown(Keys.Right))
{
KeyRight = true;
PushHorizontal += 5;
if (HPF < 3)
HPF += 2;
Face = 1;
}
else
if (Old.IsKeyDown(Keys.Right))
{
KeyRight = false;
}
if (KeyLeft == true || KeyRight == true)
{
KeyMove = true;
}
else
{
KeyMove = false;
}
if (KeyLeft == true && KeyRight == true)
{
KeyLeft = false;
KeyRight = false;
KeyMove = false;
}
if (CurrentKeys.IsKeyDown(Keys.X))
{
if (LockKeyX == false)
{
KeyJump = true;
LockKeyX = true;
PushVertical += 500;
}
if (VPF < 8)
VPF += 3;
}
else
if (Old.IsKeyDown(Keys.X))
{
KeyJump = false;
}
if (CurrentKeys.IsKeyUp(Keys.X))
{
if (Down == true)
{
LockKeyX = false;
}
}
if (CurrentKeys.IsKeyDown(Keys.Z))
{
if (LockKeyZ == false)
{
KeyDash = true;
KeyJump = false;
LockKeyZ = true;
PushHorizontal += 100;
}
if (KeyDash == true)
{
HPF = 1;
PushVertical += Gravity;
VPF = Gravity;
}
}
else
if (Old.IsKeyDown(Keys.Z))
{
KeyDash = false;
}
if (CurrentKeys.IsKeyUp(Keys.Z))
{
if (KeyMove == false && Down == true)
LockKeyZ = false;
}
if (CurrentKeys.IsKeyDown(Keys.C))
{
ChargeTime += elapsed;
if (LockKeyC == false)
{
LockKeyC = true;
TimeShot += 10f;
StayShot.ResetFrame();
}
}
else
if (Old.IsKeyDown(Keys.C))
{
if (ChargeTime > 0)
{
TimeShot += 0.4f;
ChargeTime = 0;
}
LockKeyC = false;
}
Update(elapsed);
UpdateInteraction();
if (TimeGetReady <= 0)
{
UpdateStatus(gameTime);
}
UpdateFrameStatus(elapsed);
LastStatus = RockmanStatus;
}
You need some InputManager that you update only when you need. Here is example of basic verstion of InputManager.
public override void Update(){
if(updateKeyboard) {InputManager.Update()}
}
new InputManager Class
public static class InputManager
{
public static void Update()
{
_previousKeyboardState = _currentKeyboardState;
_currentKeyboardState = Keyboard.GetState();
}
public static bool IsKeyDown(Keys key)
{
return _currentKeyboardState.IsKeyDown(key);
}
public static bool IsKeyUp(Keys key)
{
return _currentKeyboardState.IsKeyUp(key);
}
public static bool OnKeyDown(Keys key)
{
return _currentKeyboardState.IsKeyDown(key) && _previousKeyboardState.IsKeyUp(key);
}
public static bool OnKeyUp(Keys key)
{
return _currentKeyboardState.IsKeyUp(key) && _previousKeyboardState.IsKeyDown(key);
}
private static KeyboardState _currentKeyboardState;
private static KeyboardState _previousKeyboardState;
}
I have put random numbers into arrays but now I want to prevent it from being shown double in the Listbox. I already got a beginning to start with:
private bool InArray(int getal, int[] getallen, int aantal)
{
}
I think the solution is something like when the number is already in it return true and else just keep going with the code, but I can't think of how I can do this.
This is my code:
namespace Trekking
{
public partial class Form1 : Form
{
private Trekking trekking;
public Form1()
{
InitializeComponent();
btnLaatZien.Enabled = false;
btnSerie.Enabled = false;
btnSorteer.Enabled = false;
btnStart.Enabled = true;
btnStop.Enabled = false;
btnTrek.Enabled = false;
}
private void btnStart_Click(object sender, EventArgs e)
{
int AantalGewenst = Convert.ToInt32(tbInvoerAantalGewenst.Text);
int Maxwaarde = Convert.ToInt32(tbInvoerMaxwaarde.Text);
trekking = new Trekking(Maxwaarde, AantalGewenst);
btnTrek.Enabled = true;
btnStop.Enabled = true;
btnStart.Enabled = false;
if (Maxwaarde > 90)
{
MessageBox.Show("Uw getal mag niet boven de 90 zijn!");
btnStart.Enabled = true;
btnTrek.Enabled = false;
btnStop.Enabled = false;
}
else if (Maxwaarde < 0)
{
MessageBox.Show("Dit aantal is niet mogelijk!");
btnStart.Enabled = true;
btnTrek.Enabled = false;
btnStop.Enabled = false;
}
else if (AantalGewenst > 45)
{
MessageBox.Show("Uw getal mag niet boven de 45 zijn!");
btnStart.Enabled = true;
btnTrek.Enabled = false;
btnStop.Enabled = false;
}
if (AantalGewenst < 1)
{
MessageBox.Show("Dit aantal is niet mogelijk!");
btnStart.Enabled = true;
btnTrek.Enabled = false;
btnStop.Enabled = false;
}
else if (Maxwaarde / AantalGewenst < 2)
{
MessageBox.Show("Uw maxwaarde moet minstens het dubbele van Aantal Gewenst zijn!");
btnStart.Enabled = true;
btnTrek.Enabled = false;
btnStop.Enabled = false;
}
else
{
if (AantalGewenst <= 45)
btnStart.Enabled = false;
btnTrek.Enabled = true;
btnStop.Enabled = true;
}
}
public void getSingleNumber(int hoeveel)
{
int Getal = trekking.GeefGetal(hoeveel);
lbResultaat.Items.Add(Getal);
}
public void getTrekkingData()
{
for (int p = 0; p < trekking.AantalGewenst; p++)
{
int alleGetallen = trekking.GeefGetal(p);
lbResultaat.Items.Add(alleGetallen);
}
}
int count = 0;
private void btnTrek_Click(object sender, EventArgs e)
{
int gewenst = trekking.AantalGewenst;
label3.Text = Convert.ToString(count);
btnStop.Enabled = true;
btnSerie.Enabled = false;
trekking.TrekGetal();
getSingleNumber(count);
count++;
if (count == trekking.AantalGewenst)
{
MessageBox.Show("Alle gewenste trekkingen zijn uitgevoerd");
btnTrek.Enabled = false;
btnStop.Enabled = true;
btnSerie.Enabled = false;
}
}
private void btnStop_Click(object sender, EventArgs e)
{
lbResultaat.Items.Clear();
btnLaatZien.Enabled = false;
btnSerie.Enabled = false;
btnSorteer.Enabled = false;
btnStart.Enabled = true;
btnStop.Enabled = false;
btnTrek.Enabled = false;
tbInvoerAantalGewenst.Enabled = true;
tbInvoerMaxwaarde.Enabled = true;
count = 0;
}
private void tbInvoerMaxwaarde_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
e.Handled = !(Char.IsDigit(ch) || (ch == '-') || (ch < ' '));
}
private void k(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
e.Handled = !(Char.IsDigit(ch) || (ch == '-') || (ch < ' '));
}
private bool InArray(int getal, int[] getallen, int aantal)
{
}
}
}
The class:
namespace Trekking
{
class Trekking
{
//attributes
private Random random;
private int[] getallen;
//properties
public int Maxwaarde { get; private set; } //maximum waarde van te trekken getal
public int AantalGetrokken { get; private set; } //aantal getrokken getallen
public int AantalGewenst { get; private set; } //aantal te trekken getallen
public bool IsTenEinde { get; private set; } //true als alle getallen gerokken
//constructor en methoden
public Trekking(int Maxwaarde, int AantalGewenst)
{
this.Maxwaarde = Maxwaarde;
this.AantalGewenst = AantalGewenst;
AantalGetrokken = 0;
IsTenEinde = false;
random = new Random();
getallen = new int[AantalGewenst];
}
//methods
public void TrekGetal()
{
int erbijArray;
for (int i = 0; i < AantalGewenst; i++)
{
erbijArray = random.Next(1, Maxwaarde);
getallen[i] = erbijArray;
AantalGetrokken++;
}
}
public int GeefGetal(int number)
{
return getallen[number];
}
//sorteert getrokken getallen in array "getallen"
public void Sort()
{
Array.Sort(getallen);
}
}
}
I simplified your problem (leaving out min, max, max number etc).
Basically, you can keep a list of things you already encountered:
public class Lottery
{
public HashSet<int> _previousNumbers = new HashSet<int>();
private Random random = new Random();
public int GetNextNumber()
{
int next;
do
{
next = random.Next();
}
while (_previousNumbers.Contains(next));
_previousNumbers.Add(next);
return next;
}
}
A set does not contain duplicates and is efficient in its Contains implementation.