0

我正在潜入Android开发。我参与的大多数项目都是用 C# 编写的。代表是我经常使用的 C# 元素,我也使用过一些东西,比如使用扩展 EventArgs 的类或使用 set 和 get 的属性传递数据。凭借我的编程知识,我想我将能够非常顺利地开始 Android 开发。问题是我完全不知道如何处理类似于 Java 中的 C# delagte 的实现机制。

下面我将介绍一些在 C# 中运行良好的示例类,其中包含一些我想在未来的 Android 项目中使用的 C# 语言元素。有人可以为我提供此代码的翻译吗?我更喜欢使用我自己的示例,它的转换将使我能够更快地捕捉到它。此外,关于该主题的任何有价值的资源(不仅是代表,还有将 C# 转换为 Java 的一般主题)都会受到赞赏。

倒计时计时器.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleDelegateApp
{
    public class CountdownTimer
    {
        Timer tmrTicks = new Timer();
        int secondsLeft = 0;
        int numberOfSecondsToCountdown = 0;

        public bool IsWorking
        {
            get { return tmrTicks.Enabled; }
        }

        public CountdownTimer(int seconds)
        {
            if (secondsLeft < 0) secondsLeft = 0; 
            numberOfSecondsToCountdown = seconds;
            secondsLeft = seconds;

            tmrTicks.Interval = 1000;
            tmrTicks.Tick += new EventHandler(tmrTicks_Tick);
            tmrTicks.Enabled = false;
        }

        void tmrTicks_Tick(object sender, EventArgs e)
        {
            secondsLeft--;
            if (secondsLeft >= 1) 
                WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, false));
            else
            {
                Stop();
                WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, true));
            }
        }

        public void Reset()
        {
            Stop();
            secondsLeft = numberOfSecondsToCountdown;
            if (secondsLeft < 0) secondsLeft = 0;
            WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, false));
        }

        public void Stop()
        {
            tmrTicks.Enabled = false;
        }

        public void Start()
        {
            if (secondsLeft <= 0)
            {
                secondsLeft = 0;
                WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, true));
            }
            else
            {
                tmrTicks.Enabled = true;
            }
        }

        public delegate void CountdownTimerTickEventHandler(object sender, CountdownTimerEventArgs ea);

        public event CountdownTimerTickEventHandler CountdownTimerTick;

        protected virtual void WhenCountdownTimerTick(CountdownTimerEventArgs ea)
        {
            if (CountdownTimerTick != null)
            {
                CountdownTimerTick(this, ea);
            }
        }
    }

    public class CountdownTimerEventArgs : EventArgs
    {

        public string timeString = "";
        public float procentOfTimeLeft = 0.0f;
        public bool countdownFinished = false;

        public CountdownTimerEventArgs(int secondsLeft, int SecondsToCountdown, bool isfinished)
        {
            countdownFinished = isfinished;
            timeString = string.Format("{0:00}:{1:00}", secondsLeft / 60, secondsLeft % 60);
        }
    }
}

frmTest.cs

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;

namespace SampleDelegateApp
{
    public partial class frmTest : Form
    {
        CountdownTimer ctmrTest;

        public frmTest()
        {
            InitializeComponent();
            ctmrTest = new CountdownTimer(-44);
            ctmrTest.CountdownTimerTick += new CountdownTimer.CountdownTimerTickEventHandler(ctmrTest_CountdownTimerTick); 
        }

        void ctmrTest_CountdownTimerTick(object sender, CountdownTimerEventArgs ea)
        {
            lblTimer.Text = ea.timeString;
            if (ea.countdownFinished) countdownEnd();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            ctmrTest.Reset();
            ctmrTest.Start();
        }

        void countdownEnd()
        {
            MessageBox.Show("Finish");
        }
    }
}
4

3 回答 3

0

这实际上与观察者模式有关。在 .NET 中,这通过委托和其他东西变得容易,在 Java 中,您必须手动实现它,使用处理程序的接口和 args 的普通对象。查一下,你会发现很多信息。

于 2012-10-28T20:44:10.657 回答
0

我显然在我的问题中使用了一些词,这使得一切都不清楚和/或试图帮助我的人完全误解了我对这个问题的需求。到处放“代表”这个词使它变得更加复杂。在以其他方式提出另一个问题后,我得到了答案,这使我能够回答我的问题:

如何在java(Android)中实现c#事件行为

我并不是说下面的代码符合最佳编码标准,这里显示的示例在现实生活项目中有任何用途,但这正是我想要的。下面的工作示例:

tester.java

public class tester{
    public static void main(String[] args) {
        test test1 = new test(); 
        test1.start();
    }
}

测试.java

public class test implements CountdownTextTickListener {

    public test() { }

    public void start() {
        CountdownText ctx = new CountdownText(100);
        ctx.setListener(this);
        ctx.Start();
    }

    @Override
    public void CountdownTextTickEventFired(Object sender, 
                        CountdownTextTickEventArgs eventArgs) {
            System.out.println(eventArgs.TimeString);
            if(eventArgs.isStopped) System.out.println("- END -");
    }
}

CountdownTextTickListener.java

public interface CountdownTextTickListener {
     void CountdownTextTickEventFired(Object sender, CountdownTextTickEventArgs eventArgs);
}

CountdownTextTickEventArgs.java

public class CountdownTextTickEventArgs {

    public String TimeString = "";
    public boolean isStopped = false;

    public CountdownTextTickEventArgs(int seconds, boolean isStoppedState) {
        TimeString = String.format("%02d:%02d",seconds/60, seconds % 60);
        isStopped = isStoppedState;
    }

}

CountdownText.java

import java.util.Timer;
import java.util.TimerTask;

public class CountdownText {

    Timer tmrTicks = new Timer();
    int secondsLeft = 0;
    int numberOfSecondsToCountdown = 0;
    boolean isWorking = false;
    private CountdownTextTickListener listener = null;

    public boolean getIsWorking(){
        return isWorking;
    }

    public CountdownText(int seconds) {
        if (secondsLeft < 0) secondsLeft = 0; 
        numberOfSecondsToCountdown = seconds;
        secondsLeft = seconds;
    }

    void startTimer() {
        isWorking = true;
        fireEvent(secondsLeft, false);
        tmrTicks = new Timer();
        tmrTicks.scheduleAtFixedRate( new TimerTask(){
            @Override
            public void run(){
                tickTimer();
            }
        }, 1000, 1000); 
    }

    private void stopTimer() {
        isWorking = false;
        tmrTicks.cancel();
    }

    private void tickTimer() {
         secondsLeft--;
         if (secondsLeft >= 1) 
         {
             fireEvent(secondsLeft, false);
         }
         else
         {
             Stop();
             fireEvent(secondsLeft, true);
         }
    }

    public void Reset() {
        Stop();
        secondsLeft = numberOfSecondsToCountdown;
        fireEvent(secondsLeft, false);
    }

    public void Stop() {
        stopTimer();
    }

    public void Start() {
        if (secondsLeft <= 0)
        {
            secondsLeft = 0;
            fireEvent(secondsLeft, true);
        }
        else
        {
            startTimer();
        }
    }

    protected void fireEvent(int seconds, boolean isStoppedState) {
        if (listener != null) {
            Object sender = this; 
            CountdownTextTickEventArgs eventArgs = new CountdownTextTickEventArgs(seconds, isStoppedState);
            listener.CountdownTextTickEventFired(sender, eventArgs);
        }
    }

    public void setListener(CountdownTextTickListener listener) {
        this.listener = listener;
    }
}
于 2012-11-08T04:08:23.433 回答
-1

据我了解,实际上有一些方法可以让您的 C# 代码在 Android 中运行。如果您的情况允许您使用 C# 进行工作,我建议您查看适用于 Android 的 Mono 。您不仅可以将代码保留在 C# 中,还可以更轻松地将其移植到 iOS 的 MonoTouch 和 Windows Phone 的 .NET 4.5。如果没有,我无法帮助你,但我相信更有知识的人会。

于 2012-10-28T20:43:41.267 回答